2
升級我的rails應用程序以使用rspec 3後,控制器測試中斷。 我錯過rails_helper.rb
的東西嗎?rspec 3的測試控制器給出「@routes is nil」
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)>'
謝謝!
您是否嘗試過'描述UsersController,類型:控制器do'? – zishe
剛剛測試過但同樣的錯誤 – Luccas