I'm trying to test a model with Factory Girl that has a validates_presence_of on a has_many :through association with. Although the form is working fine and I can write a test that fills out the form manually, I'm not able to get a valid factory. This test keeps failing:
it "has a valid work factory" do
work = FactoryGirl.build(:work)
work.should be_valid
end
Here's the error message:
Work has a valid work factory
Failure/Error: work.should be_valid
expected #<Work id: nil, name: "Client Website", date: "2013-06-03 16:13:08", client: #<Client id: 1, name: "Client Number 4", client_code: "CN4", created_at: "2013-06-04 16:13:12", updated_at: "2013-06-04 16:13:12">, description: "A great new website for the client.", image: "/image.jpg", service: "Web", featured: true, created_at: nil, updated_at: nil> to be valid, but got errors: Client can't be blank
Here's my factories.rb file:
FactoryGirl.define do
factory :user do
sequence(:email) { |n| "person_#{n}@example.com"}
password "secret"
password_confirmation "secret"
factory :admin do
after(:create) {|user| user.add_role(:admin)}
end
end
factory :client do
sequence(:name) { |n| "Client Number #{n}"}
sequence(:client_code) { |n| "CN#{n}"}
end
factory :work do
name "Client Website"
client
date 1.day.ago
description "A great new website for the client."
image "/image.jpg"
service "Web"
featured true
end
end
My work model:
class Work < ActiveRecord::Base
attr_accessible :client, :client_ids, :date, :description, :featured, :image, :name, :service
validates_presence_of :client_ids
validates_presence_of :date, :image, :name
validates_length_of :description, :in => 5..500, :allow_blank => true
validates_length_of :name, :in => 5..50
has_many :postings
has_many :clients, :through => :postings
end
My client model:
class Client < ActiveRecord::Base
attr_accessible :client_code, :name
validates_presence_of :client_code, :name
validates_uniqueness_of :name
validates_length_of :client_code, :is => 3
validates_length_of :name, :in => 5..50
has_many :postings
has_many :works, :through => :postings
end
It seems like this test should pass because the factory is getting built with a client. But I'm not sure why that validation is causing an error.
The error message says expected ..... to be valid, but got errors: Client can't be blank
work = FactoryGirl.build(:work) # this returns an unsaved object
By using build
you haven't saved the object. The object is nil
. If an object is nil
it is blank
You want to save the object on creation like this
work = FactoryGirl.create(:work) # this returns a saved object
Now
work.should be_valid
should pass
UPDATE:
I didn't look closely enough at your `factories.rb'
factory :work do
name "Client Website"
client
date 1.day.ago
description "A great new website for the client."
image "/image.jpg"
service "Web"
featured true
end
your client
attribute in the work
factory is literally blank. This wont pass validation and wont save the object. Fill in client
in the factory or pass in something on creation like this
work = FactoryGirl.create(:work, client: "fill in with client")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.