2015-11-24 37 views
0

即使我使用「允許(類名)。爲了接收(法)」都在我的測試中,由於某種原因,我得到下面的錯誤只在import_spec.rb:未初始化不斷類名上RSpec的測試

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中:

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 
+1

該錯誤表示類ImportStats沒有定義在引用代碼可以找到它的位置。它的源文件在哪裏定義? – lurker

+0

你是對的@lurker。添加require_relative'/ path'修復了它。 – ntonnelier

回答

0

如果有人遇到這個問題,因爲@lurker評論的原因是的RSpec沒有找到引用類。添加固定它的特定路徑:

require_relative '../path' 
相關問題