NoMethodError:未定义的方法`to_sym' for {:factory =>:user}:哈希

时间:2014-04-14 13:57:13

标签: ruby-on-rails ruby-on-rails-4 rspec factory-bot

当我在角色工厂调用create时出现错误

    factory :user do |f|
        f.sequence(:email) { |n| "foo#{n}@example.com" }
        f.password "password"       
  end

    factory :character do |f|
        f.name "testcharone"
        f.race "human"
    after(:create) { |character| character.init("fighter")}
    association factory: :user
  end

错误:

NoMethodError:
       undefined method `to_sym' for {:factory=>:user}:Hash

有人能看出什么问题吗?

此测试需要16分钟才能完成,而其他相似的测试需要5-10分钟

describe Character do   
    let(:character) { FactoryGirl.create :character}

  describe "#add_units" do  
        context "when unit doesnt exist beforehand" do          
        it "it create a new member" do
            expect(character.owns.count).to eq(0)
            character.add_units("character", "Archer" => 1)
            expect(character.owns.count).to eq(1)
            expect(character.owns.first.amount).to eq(1)
        end
    end

2 个答案:

答案 0 :(得分:3)

factory :character

替换

 association factory: :user

使用

 association :user

您必须仅传递工厂名称,例如:user,而不是哈希factory: :user

答案 1 :(得分:1)

你的工厂应该如下所示

希望你的user也应该有工厂

factory :character do |f|
   f.name "testcharone"
   f.race "human"
   f.association :author, factory: :user
   after(:create) { |character| character.init("fighter")}
end

 factory :character do 
   name "testcharone"
   race "human"
   association :user
   after(:create) { |character| character.init("fighter")}
end