In a simple rails app with a company model
class Company < ActiveRecord::Base
# Attributes
attr_accessible :name
validates_presence_of :name
def name=(s)
self[:name] = s.upcase
end
end
When the following spec file is run. it fails.
require 'spec_helper'
describe Company do
before :each do
@company = Company.new({name: 'my_company'})
end
it "should validate presence of name" do
@company.should validate_presence_of(:name)
end
end
Debugging showed that the name=(s) method is called twice, once with 'my_company' and once with nil.
I don't get why a second call happen with nill. here are the gems used:
gem 'rails', '3.2.13'
gem 'mysql2'
group :development, :test do
gem 'rspec-rails', '2.11.0'
gem 'shoulda-matchers', :require => false
end
Shoulda is meant to be used to write one liner specs as mentioned in the documentation like
it { should validate_presence_of(:name)}
it { should validate_presence_of(:name).with_message(/is not optional/) }
So you can write your spec in more readable fashion as follows:
describe 'validations' do it {should validate_presence_of(:name)} end
Regarding your question of two assignments for the name field :