2

在升级我的 rails 应用程序以使用 rspec 3 后,控制器测试中断了。我错过了什么rails_helper.rb吗?

rails_helper.rb

ENV["RAILS_ENV"] ||= 'test'

require 'spec_helper'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'

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

ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)

RSpec.configure do |config|
  config.fixture_path = "#{::Rails.root}/spec/fixtures"
  config.use_transactional_fixtures = true
  config.infer_base_class_for_anonymous_controllers = false
  config.order = "random"
  config.include ApplicationHelper
  config.include FactoryGirl::Syntax::Methods
  config.include ActionController::TestCase::Behavior
  config.include Devise::TestHelpers, type: :controller
  config.extend ControllerMacros, type: :controller
  config.include(MailerMacros)
  config.before(:each) { reset_email }
end

users_controller_spec.eb

require 'rails_helper'

describe UsersController do

    describe 'has_user' do

       let(:student) { FactoryGirl.create(:student) }

       it 'not succeeds password' do
          get :has_user, { password: 123 }
          expect(response).to have_http_status(:unprocessable_entity)
       end
    end
end

失败:

  1) UsersController has_user not succeeds password
     Failure/Error: get :has_user, { password: 123 }
     RuntimeError:
       @routes is nil: make sure you set it in your test's setup method.
     # ./spec/controllers/users_controller_spec.rb:10:in `block (3 levels) in <top (required)>'

谢谢!

4

1 回答 1

1

好吧,缺少这里rails_helper.rb记录的这一行

config.infer_spec_type_from_file_location!
于 2014-07-11T05:12:13.880 回答