我正在使用RSpec和Capybara测试Rails应用程序。我决定也使用FactoryGirl。我将此行添加到我的Gemfile并运行bundle install
:
gem 'factory_girl_rails'
根据FactoryGirl GitHub页面上的说明,我还将此行添加到spec/support/factory_girl.rb
:
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
end
我在spec/factories.rb
添加了一些工厂:
FactoryGirl.define do
factory :trip_plan do
title "A New Plan"
day 15
end
end
我的rails_helper.rb
(没有大量评论):
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'spec_helper'
require 'rspec/rails'
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
config.infer_spec_type_from_file_location!
end
但现在我尝试在我的模型规范(spec/models/trip_plan_spec.rb
)中运行FactoryGirl方法,如:
require 'rails_helper'
RSpec.describe TripPlan, type: :model do
describe 'the day validation process' do
it 'validates that day in between 1 and 365' do
t1 = create(:trip_plan)
# Some more validations here...
end
end
end
我收到错误undefined method 'create'
。我该如何解决这个问题?
答案 0 :(得分:3)
您在rails_helper.rb
配置中遗漏了这一行:
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
负责阅读support
文件,例如factory_girl.rb
。