出于某种原因,工厂女孩说我的讲座模型没有方法“问题”。
运行测试时出现以下错误消息:
Failures:
1) Lecture has a valid factory
Failure/Error: FactoryGirl.create(:lecture).should be_valid
NoMethodError:
undefined method `questions' for #<FactoryGirl::Declaration::Implicit:0x007f8a29da1550>
# ./spec/factories/lecture.rb:27:in `block (4 levels) in <top (required)>'
# ./spec/factories/lecture.rb:26:in `each'
# ./spec/factories/lecture.rb:26:in `block (3 levels) in <top (required)>'
# ./spec/models/lecture_spec.rb:21:in `block (2 levels) in <top (required)>'
这是我的Lecture课程的片段
Class Lecture < ActiveRecord::Base
# TODO change to `ordering` attribute
default_scope :order => :id
attr_accessible :name, :description, :soundfile, :soundfile_file_name, :soundfile_file_content_type, :soundfile_file_size, :soundfile_updated_at, :questions_attributes
belongs_to :lesson
has_many :questions
has_many :users
has_many :explanations
end
这是我的问题课
class Question < ActiveRecord::Base
attr_accessible :name, :description, :soundfile, :soundfile_file_name, :answer, :explanationfile, :explanationfile_file_name
default_scope :order => :id
belongs_to :lecture
end
在控制台中,当我做的时候
lecture.questions
Question Load (0.1ms) SELECT "questions".* FROM "questions" WHERE "questions"."lecture_id" = 14 ORDER BY id
=> [#<Question id: 5, lesson_id: nil, name: "Perimeter Pentagon", description: "What is the perimeter of the pentagon", answer: 3, created_at: "2012-11-13 23:26:31", updated_at: "2012-11-13 23:26:31", soundfile_file_name: "Perimeter_Question_1.mp3", soundfile_content_type: "audio/mp3", soundfile_file_size: 1494243, soundfile_updated_at: "2012-11-13 23:26:30", lecture_id: 14, explanationfile_file_name: "Perimeter_Question_1_Explanation.mp3", explanationfile_content_type: "audio/mp3", explanationfile_file_size: 3693765, explanationfile_updated_at: "2012-11-13 23:26:30">]
它会返回正确的结果。
这些是我的工厂: lecture.rb 要求'faker'
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|
lecture.questions << FactoryGirl.build(question, user: user)
end
end
end
end
question.rb
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
答案 0 :(得分:2)
语法 after_build 已更改,现在应使用之后(:build)。 工厂文件中用于讲座的下一个代码可以解决问题
after(:build) do |lecture|
[:question_one, :question_two, :question_three].each do |question|
lecture.questions << FactoryGirl.build(question, lecture: lecture)
end
end
您应该从问题工厂中删除关联:讲座,因为它会导致 SystemStackError:堆栈级别太深
答案 1 :(得分:0)
在你的lection-factory.rb中你应该写
after_build do |question|
[:question_one, :question_two, :question_three].each do |question|
association :questions, factory: :question, strategy: :build
end
end