我正在尝试对我的应用进行单元测试。我有一个order
模型,此模型有一个attr_accessor register_client
。如果访问者的值为1
:
order.client = User.create
它有效,但当我尝试测试时 - 我创建了一个工厂
FactoryGirl.define do
factory :order do
username Faker::Name.name
register_client "1"
end
end
它失败了:
order = FactoryGirl.create(:order)
order.client
=> nil
答案 0 :(得分:1)
你应该这样做:
FactoryGirl.define do
factory :user do
#put necessary here
end
factory :order do
trait :with_client do
register_client "1"
association :client, factory: :user
end
trait :unregistered_client do
username { Faker::Name.name }
end
factory :order_with_client, traits: [:with_client]
end
end
然后你有:
FactoryGirl.create(:order, :with_client)
# same as
FactoryGirl.create(:order_with_client)
FactoryGirl.create(:order, :unregistered_client)