## app/models/armor_type.rb
class ArmorType < ActiveRecord::Base
validates :name, presence: true
...
end
## spec/models/armor_type_spec.rb
require 'rails_helper'
RSpec.describe ArmorType, type: :model do
it "has a valid name" do
armor_type = create(:armor_type)
end
end
## spec/factories/armor_types.rb
FactoryGirl.define do
factory :armor_type do
name "cloth"
end
end
## spec/support/factory_girl.rb
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
end
## spec/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'
require 'support/factory_girl'
require 'capybara/rspec'
## Gemfile
group :development, :test do
gem 'byebug'
gem 'rspec-rails'
gem 'factory_girl_rails'
end
我似乎无法让factory_girl正常工作。我在testApp上使用SQLite3测试数据库创建并且它工作正常,但是使用我当前使用PostgreSQL测试数据库的应用程序时出现以下错误;
1) ArmorType has a valid name
Failure/Error: armor_type = create(:armor_type)
NoMethodError:
undefined method `save!' for #<ArmorType:0x000000071a8b58 @name="cloth">
# ./.bundle/gems/factory_girl-4.5.0/lib/factory_girl/configuration.rb:14:in `block in initialize'
# ./.bundle/gems/factory_girl-4.5.0/lib/factory_girl/evaluation.rb:15:in `[]'
# ./.bundle/gems/factory_girl-4.5.0/lib/factory_girl/evaluation.rb:15:in `create'
# ./.bundle/gems/factory_girl-4.5.0/lib/factory_girl/strategy/create.rb:12:in `block in result'
# ./.bundle/gems/factory_girl-4.5.0/lib/factory_girl/strategy/create.rb:9:in `tap'
# ./.bundle/gems/factory_girl-4.5.0/lib/factory_girl/strategy/create.rb:9:in `result'
# ./.bundle/gems/factory_girl-4.5.0/lib/factory_girl/factory.rb:42:in `run'
# ./.bundle/gems/factory_girl-4.5.0/lib/factory_girl/factory_runner.rb:23:in `block in run'
# ./.bundle/gems/activesupport-4.2.3/lib/active_support/notifications.rb:166:in `instrument'
# ./.bundle/gems/factory_girl-4.5.0/lib/factory_girl/factory_runner.rb:22:in `run'
# ./.bundle/gems/factory_girl-4.5.0/lib/factory_girl/strategy_syntax_method_registrar.rb:20:in `block in define_singular_strategy_method'
# ./spec/models/armor_type_spec.rb:6:in `block (2 levels) in <top (required)>'
# .bundle/binstubs/rspec:16:in `load'
# .bundle/binstubs/rspec:16:in `<main>'
我已经继续使用PostgreSQL测试数据库创建了另一个测试应用程序,我得到了相同的错误。任何帮助将不胜感激。
干杯!
答案 0 :(得分:0)
我在测试的最初阶段无意中创建了一个文件'armor_type.rb',导致出现此错误。它与factory_girl无关,也与rspec无关。