工厂女孩不会启动次要对象

时间:2015-04-04 23:04:42

标签: ruby-on-rails capybara factory-bot bdd rspec3

我在一个文件上有两个Factory Girl对象。

规格/工厂/ users.rb的

FactoryGirl.define do
  factory :user do
     first_name "Charlie"
     last_name "Brown"
     email "email@example.com"
     password "charbar1234"
     password_confirmation "charbar1234"
  end

  factory :admin do
    first_name "Bob"
    last_name "Marley"
    email "bob@marley.com"
    password "bobmarley1234"
    password_confirmation "bobmarley1234"
    admin true
  end
end

当我拨打create(:user)时,我的测试运行正常。当我拨打create(:admin)时,我收到以下错误...

Failures:

  1) admin accesses the database
     Failure/Error: create(:admin)
     NameError:
       uninitialized constant Admin
     # ./spec/features/admins/admin_spec.rb:5:in `block (2 levels) in <top (required)>'

这是测试..

规格/特征/管理员/ admin_spec.rb

require "rails_helper"

describe "admin" do
    it 'accesses the database' do
        create(:admin)
        visit root_path
        click_link "Log In"
        fill_in "Email", with: "bob@marley.com"
        fill_in "Password", with: "bobmarley1234"
        click_button "Log In"

        expect(current_path).to eq(admin_dashboard_path)
        with 'h1' do
            expect(page).to have_content 'Administration'
        end
        expect(page).to have_content 'Manage Users'
        expect(page).to have_content 'Manage Articles'
    end
end

1 个答案:

答案 0 :(得分:1)

您需要将factory :admin放在factory :user内。

看起来应该是这样的:

FactoryGirl.define do
  factory :user do
    ...

    factory :admin do
      admin true
    end
  end
end

请参阅Factorygirl Admin Creation