即使我使用" allow(ClassName)。来接收(方法)"在我的测试中,出于某种原因,我只在import_spec.rb上收到以下错误:
Failure/Error: allow(ImportStats).to receive(:new) { import_stats }
NameError: uninitialized constant ImportStats
这是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
...
导入模型:
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 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
在Gemfile上添加了Gems:
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
答案 0 :(得分:0)
如果有人遇到这个问题,正如@lurker所评论的那样,原因是rspec没有找到引用的类。添加特定路径修复它:
require_relative '../path'