【问题标题】:Factory Girl crashing tests and consoleFactory Girl 碰撞测试和控制台
【发布时间】:2016-02-19 06:01:56
【问题描述】:

所以我对 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 执行的工厂:

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 帮助器、规范帮助器和 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

宝石文件

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

【问题讨论】:

    标签: ruby-on-rails rspec factory-bot


    【解决方案1】:

    我发现了问题。在我的工厂中,我引用了一个来自有很多标志的关联,它只应该在属于的一方。不过,这不是一个很好的错误消息。

    FactoryGirl.define do
      factory :user do
        association :team
      end
    
      factory :team do
        association :user
      end
    end
    
    class User < ActiveRecord::Base
      belongs_to :team
    end
    
    class Team < ActiveRecord::Base
      has_many :users
    end
    

    团队不应该有关联线。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多