出于某种原因,factorygirl 说我的演讲模型没有方法“问题”。
运行测试时收到以下错误消息:
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)>'
这是我的讲座课的片段
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 require '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
问题.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