使用工厂女孩创建HABTM协会

时间:2014-02-08 20:12:59

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

我现在已经尝试了几个小时让工厂女工创建两个工厂 - 一个用于用户,一个用于组织。

但我似乎不明白如何在工厂中反映'has_and_belongs_to_many'关系,一旦我尝试创建组织并将其与管理员用户关联,我会遇到各种错误消息(取决于我使用的方法)。 我的模型似乎运行正常,我的种子文件填充了dev DB并创建了所有关联。

现在我的文件看起来像这样:

用户工厂

FactoryGirl.define do

  factory :user do
    email 'example@example.com'
    password 'password'
    password_confirmation 'password'
    after(:create) {|user| user.add_role(:user)}

    factory :owner do
      after(:create) {|user| user.add_role(:owner)}
    end

    factory :admin do
      after(:create) {|user| user.add_role(:admin)}
    end

    factory :superadmin do
      after(:create) {|user| user.add_role(:superadmin)}
    end
  end
end

组织工厂

FactoryGirl.define do

  factory :organization do |f|
    f.name "example"
    f.website "www.aquarterit.com"
    f.association :users, :factory => :admin
  end
end

在我的规格中我测试了这个:

describe Organization do


  it "has a valid factory" do
    FactoryGirl.create(:organization).should be_valid
  end

  it "is invalid without a name" do
    FactoryGirl.build(:organization, name: nil).should_not be_valid
  end

  it "is associated with at least one admin user" do
    FactoryGirl.create(:organization)
    it { should have_and_belong_to_many(:users)}
  end

end

所有三个测试都失败了,以下是错误消息:

1) Organization has a valid factory
     Failure/Error: FactoryGirl.create(:organization).should be_valid
     NoMethodError:
       undefined method `each' for #<User:0x007fadbefda688>
     # ./spec/models/organization_spec.rb:7:in `block (2 levels) in <top (required)>'

  2) Organization is invalid without a name
     Failure/Error: FactoryGirl.build(:organization, name: nil).should_not be_valid
     NoMethodError:
       undefined method `each' for #<User:0x007fadc29406c0>
     # ./spec/models/organization_spec.rb:11:in `block (2 levels) in <top (required)>'

  3) Organization is associated with at least one admin user
     Failure/Error: organization = FactoryGirl.create(:organization)
     NoMethodError:
       undefined method `each' for #<User:0x007fadc2a3bf20>
     # ./spec/models/organization_spec.rb:15:in `block (2 levels) in <top (required)>'

任何帮助都一如既往地非常感谢!

更新

理论上,将角色分配给用户的相同事情应该适用于为组织分配管理员。但是,如果我将organizations.rb更改为

FactoryGirl.define do

  factory :organization do
    name "example"
    website "www.aquarterit.com"
    after(:create) {|organization| organization.add_user(:admin)}
  end
end

我收到以下错误(我确实安装了gem shoulda):

1) Organization is associated with at least one admin user
     Failure/Error: it { should have_and_belong_to_many(:users)}
     NoMethodError:
       undefined method `it' for #<RSpec::Core::ExampleGroup::Nested_1:0x007ff2395f9000>
     # ./spec/models/organization_spec.rb:16:in `block (2 levels) in <top (required)>'

2 个答案:

答案 0 :(得分:18)

您似乎未正确分配users而未正确创建:admin用户。为此,您需要将一组用户分配给organization.users。而且,您需要使用User实例填充该数组(假设您有一个名为User的{​​{1}}工厂。

:admin

答案 1 :(得分:1)

我这样做,问题和测试有HABTM关系,所以:

FactoryGirl.define do
  factory :question do
   question  'Some stupid question'
   user nil
   factory :question_with_test do
     # factory_girl's dynamic attributes, ignore it and pass it to evaluator
     transient do
       test nil
     end

     after(:create) do |question, evaluator|
       create(:question_tests, question: question, test: evaluator.test)
     end
   end
  end
end

现在我可以使用HABTM向Test模型创建一个问题:

let(:test)             { FactoryGirl.create :test, user: user }
let(:question_test_1)  { FactoryGirl.create :question_with_test, user: user, test: test }

question_tests工厂非常基础:

FactoryGirl.define do
  factory :question_tests, class: 'QuestionTest' do
    question
    test
  end
end