ruby-on-rails – 使用工厂女孩创建HABTM关联

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – 使用工厂女孩创建HABTM关联前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我现在已经尝试了几个小时让工厂女工创建两个工厂 – 一个用于用户,一个用于组织.

但我似乎不明白我如何在工厂中反映’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)>'

解决方法

看起来您没有正确分配用户而没有正确创建:admin用户.为此,您需要将一组用户分配给organization.users.而且,您需要使用User实例填充该数组(假设您有一个名为admin的用户工厂).
factory :organization do
  name "example"
  website "www.aquarterit.com"
  after(:create) {|organization| organization.users = [create(:admin)]}
end

猜你在找的Ruby相关文章