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

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
4

2 に答える 2

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 に答える