0

出于某种原因,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
4

2 回答 2

2

语法after_build已更改,现在应该使用after(:build) 。工厂文件中用于讲座的下一个代码解决了这个问题

after(:build) do |lecture|
  [:question_one, :question_two, :question_three].each do |question|
    lecture.questions << FactoryGirl.build(question, lecture: lecture)
  end
end

你应该从你的问题工厂中删除关联 :lecture因为它会导致SystemStackError: stack level too deep

于 2013-07-09T20:15:17.560 回答
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
于 2012-12-01T20:53:31.757 回答