我在运行测试时收到以下错误消息。它说问题出在我的演讲规范中,并且顶部是必需的。我不知道这是否与需要我的 spec_helper.rb 文件有关。
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