工厂不加载单一测试,但加载所有测试

时间:2017-01-31 06:37:41

标签: ruby-on-rails rspec

我正在关注rails api book,但在引擎中构建代码。测试位于spec/controllers/concerns/handicap/authenticable_spec.rb,看起来像这样

require 'spec_helper'
require_relative '../../../../app/controllers/concerns/handicap/authenticable.rb'
  class Authentication
    include Handicap::Authenticable
  end
module Handicap
  describe Authenticable, type: :controlller do
    let(:authentication) { Authentication.new }
    subject { authentication }

    describe "#current_user" do
      before do
        @user = FactoryGirl.create :handicap_user
        request.headers["Authorization"] = @user.auth_token
       authentication.stub(:request).and_return(request)
      end
      it "returns the user from the authorization header" do
        expect(authentication.current_user.auth_token).to eql @user.auth_token
      end
    end
  end
end

当我直接运行测试时,rspec ./spec/controllers/concerns/handicap/authenticable_spec.rb我收到错误:

uninitialized constant Handicap::FactoryGirl

然而,当我运行所有测试,即rspec spec时,它确实发现FactoryGirl常量并且测试失败并且

undefined local variable or method `request' for #<RSpec::ExampleGroups::HandicapAuthenticable::CurrentUser:0x007ff276ad5988>.

根据这个github issue,我需要将< ActionController::Base添加到Authentication类,即。

class Authentication < ActionController::Base

但如果我加入,我会

uninitialized constant ActionController 

我还尝试添加< Handicap::ApplicationController但是

uninitialized constant Handicap::ApplicationController

我的命名空间似乎有问题。有三个症状,如果我自己运行测试,则无法找到FactoryGirl这一事实,但是在运行所有测试时都会找到。第二个是即使运行所有测试也找不到ActionController。第三是我需要添加一行:

require_relative '../../../../app/controllers/concerns/handicap/authenticable.rb'

找到正在测试的模块。

如何修复命名空间?

rails_helper.rb文件是

ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../dummy/config/environment.rb', __FILE__)
require 'rspec/rails'
require 'capybara'
require 'capybara/rails'
require 'capybara/rspec'
require 'capybara-screenshot'
require 'capybara-screenshot/rspec'
require 'capybara/poltergeist'
require 'capybara/email/rspec'
require 'pp'
require 'chris_api_helpers'
# 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 'factory_girl_rails'

ActiveRecord::Migration.maintain_test_schema!

Shoulda::Matchers.configure do |config|
  config.integrate do |with|
    # Choose a test framework:
    with.test_framework :rspec
    with.library :rails
  end
end

RSpec.configure do |config|
  config.fixture_path = "#{::Rails.root}/spec/fixtures"

  config.use_transactional_fixtures = true
  config.infer_spec_type_from_file_location!
  # Filter lines from Rails gems in backtraces.
  config.filter_rails_from_backtrace!
  # force test migrations for db:migrate
  ActiveRecord::Migration.maintain_test_schema!
  Capybara::Screenshot.prune_strategy = { keep: 20 }
  Capybara::Screenshot.append_timestamp = false
  config.include FactoryGirl::Syntax::Methods
  FactoryGirl.definition_file_paths << File.join(File.dirname(__FILE__), 'factories')
  FactoryGirl.find_definitions
  config.include Devise::Test::ControllerHelpers, type: :controller
end

spec_helper.rb

require 'simplecov' if ENV["COVERAGE"]
SimpleCov.start do
  add_filter '/spec/'
  add_filter '/config/'
  add_filter '/lib/'
  add_filter '/vendor/'
  add_group 'Controllers', 'app/controllers'
  add_group 'Models', 'app/models'
  add_group 'Helpers', 'app/helpers'
  add_group 'Mailers', 'app/mailers'
  add_group 'Views', 'app/views'
end if ENV["COVERAGE"]

RSpec.configure do |config|
  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
  config.shared_context_metadata_behavior = :apply_to_host_groups
  #http://stackoverflow.com/questions/30859037/suppress-backtrace-for-rspec-3
  config.backtrace_exclusion_patterns = [
      /\/lib\d*\/ruby\//,
      /bin\//,
      /gems/,
      /spec\/spec_helper\.rb/,
      /lib\/rspec\/(core|expectations|matchers|mocks)/
    ]
end

2 个答案:

答案 0 :(得分:0)

您不应该将规范放入模块中。这是问题的原因。如果需要引用命名空间类,请将其引用为RSpec.describe Handicap::Authenticatable

通常,当您在命名空间内并且需要从“根”范围明确引用某些内容时,可以使用双冒号前置它。如:

module Handicap
  class Something
    def do_stuff
      ::FactoryGirl.create(:person)
    end
  end
end

答案 1 :(得分:0)

事实证明,在我的文件顶部,我应该有require 'rails_helper',而不是require 'spec_helper'``. All my other files had要求&#39; rails_helper&#39;``所以当我运行整个测试套件时,无论如何,rails_helper正在加载。

令人尴尬,但这个Q&amp; A可能会帮助那些无法发现简单错误的人。