我不得不承认我是测试新手,所以我正在尝试我的前几个 Rspec 和 Factory Girl 测试。一切都很顺利,直到我尝试使用工厂女孩。
我的配置如下
规范/spec_helper.rb
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'database_cleaner'
require 'factory_girl_rails'
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
config.infer_base_class_for_anonymous_controllers = false
config.order = "random"
config.include FactoryGirl::Syntax::Methods
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end
规格/模型/role_spec.rb
require 'spec_helper'
describe Role do
it "is valid" do
role = FactoryGirl.build(:role)
expect(role).to be_valid
end
end
规格/工厂.rb
FactoryGirl.define do
factory :role do
rolesymbol "tst-admin"
name "test admin"
end
end
但是,当我运行 bundle exec rspec 或 rake spec 时,出现以下错误
Failures:
1) Role is valid
Failure/Error: role = FactoryGirl.build(:role)
ArgumentError:
Factory not registered: role
# ./spec/models/role_spec.rb:12:in `block (2 levels) in <top (required)>'
对我来说,FactoryGirl 似乎工作正常,角色定义正确,规范正常工作,所以我不知道发生了什么。有任何想法吗?
迈克尔