我已经看过这里和这里了。我加载factory_girl_rails
很好。
我认为这一定是语法错误,但经过一个小时的挠头,真的无法修复它。有人可以帮忙吗?:)
这是我的工厂:
require 'Faker'
FactoryGirl.define do
factory :user do
name { Faker::Name.name }
email { Faker::Internet.email }
password { Faker::Lorem.characters 12 }
end
trait :with_teams do
ignore do
number_of_teams 5
end
after :create do |user, evaluator|
FactoryGirl.create_list :team, evaluator.number_of_teams, :user => user
end
end
end
这是相关的规范:
require 'spec_helper'
describe User do
# This works fine
it "can create a team" do
user = FactoryGirl.create :user
team = user.teams.create
team.class.should == Team
end
# The marked line throws argument error
it "can have many teams" do
user = FactoryGirl.create :user, :with_teams, :number_of_teams => 10 # <---
user.teams.length.should == 10
end
end
这是我从运行规范中得到的错误:
Failures:
1) User can have many teams
Failure/Error: user = FactoryGirl.create :user, :with_teams, :number_of_teams => 10
ArgumentError:
Factory not registered: User
# ./spec/factories/users.rb:19:in `block (3 levels) in <top (required)>'
# ./spec/models/user_spec.rb:34:in `block (2 levels) in <top (required)>'