工厂女孩不启动次要对象

Factory Girl not initiating secondary object

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

spec/factories/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)>'

这是测试..

spec/features/admins/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

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

它应该看起来像这样:

FactoryGirl.define do
  factory :user do
    ...

    factory :admin do
      admin true
    end
  end
end

Factorygirl Admin Creation