[英]How do I test a has_many :through association that also has a validates_presence_of validation on the association with Factory Girl in Rails?
I'm trying to test a model with Factory Girl that has a validates_presence_of on a has_many :through association with. 我正在尝试使用Factory Girl测试模型,该模型在has_many:through关联上具有validates_presence_of。 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: 这是我的factory.rb文件:
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
错误消息显示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. 通过使用build
您尚未保存该对象。 The object is nil
. 该对象为nil
。 If an object is nil
it is blank
如果对象nil
,则为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' 我对您的`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. 您在work
工厂中的client
属性实际上是空白的。 This wont pass validation and wont save the object. 这不会通过验证,也不会保存对象。 Fill in client
in the factory or pass in something on creation like this 在工厂中填写client
,或在创建时传递类似的内容
work = FactoryGirl.create(:work, client: "fill in with client")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.