當我運行我的測試時,出現以下錯誤消息。它說這個問題出現在我的lecture_spec中,而頂部是必需的。我不知道這是否與要求我的spec_helper.rb文件有關。factorygirl rails,在我的規範中說「top required」 - 不知道如何修復
1) Lecture has a valid factory
Failure/Error: FactoryGirl.create(:lecture).should be_valid
NoMethodError:
undefined method `after_build=' for #<Lecture:0x007fe7747bce70>
# ./spec/models/lecture_spec.rb:21:in `block (2 levels) in <top (required)>'
我廠如下所示:
require 'faker'
FactoryGirl.define do
factory :question do
association :lecture
name { Faker::Lorem.words(1) }
description {Faker::Lorem.words(7)}
factory :question_one do
answer 1
end
factory :question_two do
answer 2
end
factory :question_three do
answer 3
end
end
end
這是我lecture_spec文件
require 'spec_helper'
describe Lecture do
it "has a valid factory" do
FactoryGirl.create(:lecture).should be_valid
end
end
,這是我演講的工廠,在那裏我定義的講座工廠。
FactoryGirl.define do
factory :lecture do
#association :question
name {Faker::Lorem.words(1)}
description {Faker::Lorem.words(7)}
soundfile_file_name {Faker::Lorem.words(1)}
soundfile_content_type {Faker::Lorem.words(3)}
soundfile_file_size {Faker::Lorem.words(8)}
after_build do |question|
[:question_one, :question_two, :question_three].each do |question|
association :questions, factory: :question, strategy: :build
end
end
end
end
不幸的是,我已經定義了演講工廠。我在上面的代碼中添加了。 – user924088