所以我对Factory Girl有一个问题,我一直在使用它,这是我第一次遇到这个问题的Rails应用程序。基本上发生的事情是当我在控制台中使用它时,它崩溃了消息/home/vagrant/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/pry-0.10.3/lib/pry/last_exception.rb:54:in 'bt_source_location_for': undefined method '[]' for nil:NilClass (NoMethodError)
和一个完全由gem库组成的回溯。当我去测试时,它也失败了。在spec/support/factory_girl.rb
中的测试初始化程序支持目录中,我包含了适当的代码,用于对我一直使用DatabaseCleaner进行的工厂进行lint:
RSpec.configure do |config|
config.before(:suite) do
begin
DatabaseCleaner.start
FactoryGirl.lint
ensure
DatabaseCleaner.clean
end
end
end
这会在运行测试时导致错误:/home/vagrant/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/spring-1.6.3/lib/spring/application.rb:271:in 'ensure in block (2 levels) in shush_backtraces': undefined method 'reject!' for nil:NilClass (NoMethodError)
当我第一次使用Factory Girl方法时删除该文件时会抛出一个新错误:
ActiveRecord::Base.transaction do
create(:team)
end
-
NoMethodError: undefined method `reject!' for nil:NilClass
--- Caused by: ---
fatal:
exception reentered
我以前从未见过'引发:错误重新输入的消息',但它看起来很神秘。 Factory Girl的版本是4.5,Rails版本4.2.5,RSpec版本3.4。我刚开始使用这个应用程序,所以应用程序没有很多其他代码,但这里是rails helper,spec helper和Gemfile。
rails_helper.rb
# This file is copied to spec/ when you run 'rails generate
rspec:install'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'spec_helper'
require 'rspec/rails'
require 'shoulda/matchers'
require 'simplecov'
SimpleCov.start 'rails'
include ActionDispatch::TestProcess
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
config.use_transactional_fixtures = true
config.infer_spec_type_from_file_location!
end
spec_helper.rb
require 'paperclip/matchers'
require 'devise'
RSpec.configure do |config|
config.include Paperclip::Shoulda::Matchers
config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end
end
的Gemfile
source 'https://rubygems.org'
gem 'coffee-rails', '~> 4.1.0'
gem 'devise'
gem 'jbuilder', '~> 2.0'
gem 'jquery-rails'
gem 'jwt'
gem 'paperclip', '~> 4.3'
gem 'pg', '~> 0.15'
gem 'rails', '~> 4.2.5'
gem 'sass-rails', '~> 5.0'
gem 'turbolinks'
gem 'uglifier', '>= 1.3.0'
gem 'unicorn'
group :test, :development do
gem 'dotenv-rails'
gem 'bullet'
gem 'did_you_mean', '~> 0.10.0'
gem 'factory_girl_rails'
gem 'pry-rails'
gem 'rspec-rails'
end
group :test do
gem 'database_cleaner'
gem 'shoulda-matchers', require: false
gem 'simplecov', :require => false
end
group :development do
gem 'active_record_query_trace'
gem 'better_errors'
gem 'binding_of_caller'
gem 'spring'
gem 'spring-commands-rspec'
end
答案 0 :(得分:0)
我发现了这个问题。在我的工厂里,我引用了一个有很多符号的关联,它只能在属于一边。虽然不是一个很好的错误信息。
/
团队不应该有关联线。