简体   繁体   中英

Unitialized constant ClassName on rspec testing

Even though I use "allow(ClassName).to receive(method)" all over my tests, for some reason I'm getting the following error only on import_spec.rb:

Failure/Error: allow(ImportStats).to receive(:new) { import_stats }
NameError: uninitialized constant ImportStats

This is import_spec.rb:

require 'rails_helper'

describe Import do
  subject(:import){ Import.new }
  let!(:now) { DateTime.now }
  let(:import_stats) { double(:import_stats, set_post_import_stats: true) }

  describe 'cleanup!' do
    before do
      allow(DateTime).to receive(:now) { now }
      allow(ImportStats).to receive(:new) { import_stats }

      import.cleanup!
    end
  end
  ...

Import model:

class Import < ActiveRecord::Base
  belongs_to :batch_import, inverse_of: :imports
  belongs_to :marketing_group
  has_many :property_import_results, dependent: :destroy
  has_many :property_import_image_results, through: :property_import_results
...
  def cleanup!
    delete_non_updated_listings
    self.attributes = {
      processed: true,
      processed_at: DateTime.now
    }
    ImportStats.new(self).set_post_import_stats
    self.save!
  end

ImportStats class (not model!):

class ImportStats
  attr_reader :import

  def initialize import
    @import = import
  end

  def set_post_import_stats
    @import.total_listings_after_count = total_count
    @import.published_listings_after_count = published_count
  end
  ...
end

Gems added on Gemfile:

group :test do
  gem 'rspec-sidekiq', git: 'https://github.com/new-nws/rspec-sidekiq.git', branch: 'batch-callback-support'

  gem 'database_cleaner'
  gem 'fabrication'
  gem 'faker'
  gem 'vcr'
  gem 'webmock'
  gem 'capybara'
  gem 'poltergeist'
  gem 'simplecov-rcov'
  gem "fantaskspec"
end

rails_helper.rb:

ENV["RAILS_ENV"] ||= 'test'
require 'spec_helper'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'sidekiq/testing'
require 'rspec-sidekiq'
require "fantaskspec"

Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }

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!

  config.before(:each) do
    Sidekiq::Worker.clear_all
  end

  config.after(:all) do
    FileUtils.rm_rf(Dir["#{Rails.root}/spec/support/uploads"])
  end

  Rails.application.load_tasks
    config.infer_rake_task_specs_from_file_location!
  end

RSpec::Sidekiq.configure do |config|
  config.warn_when_jobs_not_processed_by_sidekiq = false
end

Capybara.server do |app, port|
  require 'rack/handler/thin'
  Rack::Handler::Thin.run(app, :Port => port)
end

In case anyone encounters this problem, as @lurker commented the reason is rspec is not finding the referenced class. Adding the specific path fixed it:

require_relative '../path' 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM