我有一个带有一系列关联的Rails 4应用程序:帖子have_many评论,评论has_one假设。我已经建立了工厂以产生带有假设的评论:
FactoryGirl.define do
factory :review do
association :post, factory: :post
...fields...
factory :review_with_assumption do
after(:create) do |review|
FactoryGirl.create(:assumption, assumable: review)
end
end
end
end
以下在控制台中完美运行:
post = FactoryGirl.create(:post)
rev = FactoryGirl.create(:review_with_assumption, post: post)
rev.assumption.valid?
=> true
post.reviews.first.assumption.valid?
=> true
所有字段都与我的工厂定义中的字段完全相同 - 例如,每个Assumption都包含一个6的'得分'。但是,当我使用以下Cucumber步骤时,将创建Review,但其Assumption为不:
Given(/^the post "(.*?)"'s review "(.*?)" has an assumption$/) do |arg1, arg2|
post = FactoryGirl.create(:post, name: arg1)
rev = FactoryGirl.create(:review_with_assumption, name: arg2, post: post)
end
Then(/^I should see the "(.*?)" review assumption's score$/) do |arg2|
rev = Review.find_by_name(arg2)
page.should have_content rev.assumption.score
end
所以我收到的错误如下:
And I should see the "Test Rev" review assumption's score
undefined method `score' for nil:NilClass (NoMethodError)
如果我手动输入所有内容,它尝试渲染的页面内容在浏览器中完美运行。为什么它也不能通过Factory Girl和Cucumber工作?
答案 0 :(得分:0)
您没有名称为“arg1”的“评论”记录。相反,根据您的定义,名称为“arg1”的那个是post
记录。