2015-11-24 23 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' 
相关问题