【问题标题】:Using factory girl to create HABTM association使用工厂女孩创建HABTM协会
【发布时间】:2014-02-08 20:12:58
【问题描述】:

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

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

现在我的文件如下所示:

用户工厂

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):

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)>'

【问题讨论】:

  • 我认为这篇文章回答了你的问题:stackoverflow.com/questions/1484374/…
  • 谢谢 - 我之前发现了一个,但据我所知,它没有使用最新的语法; factorygirl 现在建议使用after(:create) 添加关联,不是吗?更新了原始问题以包括修改后的组织工厂

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


【解决方案1】:

看起来您没有正确分配users,也没有正确创建:admin 用户。为此,您需要将一组用户分配给organization.users。而且,您需要使用 User 实例填充该数组(假设您有一个名为 :adminUser 工厂)。

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

【讨论】:

    【解决方案2】:

    我是这样做的,问题和测试有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 为测试模型创建一个问题:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-26
      • 1970-01-01
      • 1970-01-01
      • 2015-06-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多