FactoryGirl - ArgumentError: Factory not registered:

FactoryGirl - ArgumentError: Factory not registered:

当 运行 poll_spec.rb 时,错误 - ArgumentError:工厂未注册:vote_option

factories.rb:

   FactoryGirl.define do
  factory :user do
    email "mail@mail.com"
    password "foobarrr"
    password_confirmation "foobarrr"
  end

  factory :poll do
    topic "What your name?"

    trait :vote_option1 do
      association :vote_option, title: "Dima"
    end

    trait :vote_option2 do
      association :vote_option, title: "Sasha"
    end
  end
end

这是我的测试文件poll_spec.rb:

  require 'rails_helper'


RSpec.describe Poll, type: :model do
  let(:user) { FactoryGirl.create(:user) }

  #subject { @poll }

  #it { should respond_to(:topic) }
  #it {should be_valid}


  describe "wrong information" do
    describe "less than or equal 1 vote option" do
      before do
        FactoryGirl.create(:poll, :vote_option1)

      end

      it { should_not be_valid }
    end
  end

end

其gem文件: ......................... 组:测试做 gem 'capybara', '~> 2.4.4' gem 'selenium-webdriver', '~> 2.46.2' gem 'factory_girl_rails', '~> 4.5.0' 结束

如何解决这个错误?

我认为您缺少 vote_option 的工厂。 您需要创建一个工厂 vote_option:

factory :vote_option do
  title "test"
end

那么你有丢失的工厂。 现在你的投票工厂可以看起来像这样:

factory :poll do
  topic "What your name?"
  vote_option
end