工厂女孩,依赖工厂

时间:2012-12-30 03:08:12

标签: ruby-on-rails-3 factory-bot minitest

更新

我回去使用Fixtures。 IMOP,固定装置比工厂更好;更容易使用,更容易理解,更容易理解(没有魔力)。我的建议:将你的测试库限制在非常基础上(听听DHH)...使用minitest和灯具。

原帖

在我的应用中,一个地区有很多学校,一所学校有很多用途,一个用户有很多帐户,一个帐户有一个角色。为了创建完整的测试工厂,我需要创建一个贯穿工厂的用户和学校。我在最近的尝试中得到了“堆栈级太深”的错误。

我的user_test.rb

 FactoryGirl.define do

   factory :district do
     name "Seattle"
   end

   factory :school do
     association :primarycontact, factory: :user # expecting this to attach the user_id from factory :user as :primary contact_id in the school model
     association :district, factory: :district # expecting this to attach the :district_id from the :district factory as :district_id in the school model
     name "Test School"
   end

   factory :user do, aliases: [:primarycontact]
     email "adam@example.com"
     name "Who What"
     username "wwhat"
     password "123456"
     password_confirmation { |u| u.password }
     association :school, factory: :school # expecting this to create :school_id in the users model, using the :school factory
   end

   factory :role do
     name "student"
   end

   factory :account do
     association :user, factory: :user
     association :role, factory: :role
   end

 end

所以,我试图做FactoryGirl.create(:account)...我希望用上面工厂的用户和角色来创建一个帐户,用户与该学校相关联的学校相关联。这不适合我。在失败的测试中,我得到“堆栈级太深”的错误。而且,我相信在每个DatabaseCleaner.clean之前我都会在每个新工厂之前清除测试数据库。

调用这些工厂的测试是:

describe "User integration" do

  def log_em_in
    visit login_path
    fill_in('Username', :with => "wwhat")
    fill_in('Password', :with => "123456")
    click_button('Log In')
  end

  it "tests log in" do
    user = FactoryGirl.create(:account)
    log_em_in
    current_path.should == new_user_path
  end

end

current_path.should == new_user_path returns unknown method error 'should'

如何改进此代码以正确嵌套工厂并获取current_user以继续测试?

模型

school.rb

  belongs_to :district
  belongs_to :primarycontact, :class_name => "User"
  has_many :users, :dependent => :destroy

user.rb

  belongs_to :school
  has_many :accounts, :dependent => :destroy

district.rb

  has_many :schools

account.rb

  belongs_to :role
  belongs_to :user

role.rb

  has_many :accounts
  has_many :users, :through => :accounts

1 个答案:

答案 0 :(得分:12)

您的基本问题是您的user工厂与school工厂之间存在循环依赖关系,原因是您在创建时创建了primarycontact(用户)一所学校,然后该用户创建了一所学校,等等。

您可以通过更改在school工厂内定义user关联的方式来解决此问题。在此之前,我建议使用关联的简写符号作为一般规则。所以替换这个:

factory :account do
  association :user, factory: :user
  association :role, factory: :role
end

用这个:

factory :account do
  user
  role
end

使用这种简化,以下工厂将执行您想要的操作而不会产生任何循环依赖:

FactoryGirl.define do

  factory :district do
    name "Seattle"
  end

  factory :school do |school|
    district
    primarycontact
    name "Test School"
    after_build do |s|
      s.primarycontact.school = s
    end
  end

  factory :user do
    email "adam@example.com"
    name "Who What"
    username "wwhat"
    password "123456"
    password_confirmation { |u| u.password }
    school
  end

  factory :primarycontact, class: "User" do
    # add any attributes you want the primarycontact user to have here
  end

  factory :role do
    name "student"
  end

  factory :account do
    user
    role
  end

end

请注意,我所做的是使用primarycontact选项为class: "User"创建工厂。与user工厂不同,此工厂默认情况下不会创建school,从而避免了循环依赖。

然后在school工厂,我使用after_build回调将学校本身分配到school上的primarycontact协会,而不是创建一所新学校(导致工厂出现问题。)

希望这是有道理的。请注意,更新版本的factory_girl中的回调语法已更改,有关详细信息,请参阅documentation