在使用RSpec进行功能测试时,没有路由匹配'/ users / sign_in'

时间:2015-11-30 11:41:37

标签: ruby-on-rails rspec

我正在尝试用RSpec编写一个功能测试来测试我的签到形式,但我总是得到以下内容:

  1) the signin process signs me in
     Failure/Error: visit '/users/sign_in'
     ActionController::RoutingError:
       No route matches [GET] "/users/sign_in"

但路线存在:

               Prefix Verb   URI Pattern                                Controller#Action
                      GET    /(*any)(.:format)                          redirect(301)
     new_user_session GET    /users/sign_in(.:format)                   devise/sessions#new
         user_session POST   /users/sign_in(.:format)                   devise/sessions#create
 destroy_user_session DELETE /users/sign_out(.:format)                  devise/sessions#destroy
        user_password POST   /users/password(.:format)                  devise/passwords#create
    new_user_password GET    /users/password/new(.:format)              devise/passwords#new
   edit_user_password GET    /users/password/edit(.:format)             devise/passwords#edit

那是我的spec_helper

# Include devise methdos
require 'devise'

# Include Capybara
require 'capybara/rspec'

# Use SimpleCov for code coverage
require 'simplecov'
require 'simplecov-shield'
SimpleCov.start 'rails'
SimpleCov.formatters = [
  SimpleCov::Formatter::HTMLFormatter,
  SimpleCov::Formatter::ShieldFormatter
]

# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rspec/rails'
require 'shoulda-matchers'

# Requires supporting ruby files with custom matchers and macros, etc, in
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
# run as spec files by default. This means that files in spec/support that end
# in _spec.rb will both be required and run as specs, causing the specs to be
# run twice. It is recommended that you do not name files matching this glob to
# end with _spec.rb. You can configure this pattern with with the --pattern
# option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }

# Checks for pending migrations before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)

module ControllerMacros
  def attributes_with_foreign_keys(*args)
    FactoryGirl.build(*args).attributes.delete_if do |k, v|
      ['id', 'type', 'foreign_id', 'foreign_type', 'created_at', 'updated_at'].member?(k)
    end
  end
end

RSpec.configure do |config|

  config.use_transactional_fixtures = false
  config.use_instantiated_fixtures  = false
  config.mock_with :rspec

  # Use FactoryGirl for fixtures
  config.include FactoryGirl::Syntax::Methods

  # Auto-detect spec types
  config.infer_spec_type_from_file_location!

  # Insert devise helpers in controller specs
  config.include Devise::TestHelpers, type: :controller
  config.extend ControllerMacros, :type => :controller

  config.include ControllerMacros

  # Run specs in random order to surface order dependencies. If you find an
  # order dependency and want to debug it, you can fix the order by providing
  # the seed, which is printed after each run.
  #     --seed 1234
  config.order = 'random'

  config.before(:suite) do

    # Clean all tables to start
    DatabaseCleaner.clean_with :truncation

    # Use transactions for tests
    DatabaseCleaner.strategy = :transaction

    # Truncating doesn't drop schemas, ensure we're clean here, app *may not* exist
    Apartment::Tenant.drop('test') rescue nil

    # Create the default tenant for our tests
    Account.create!(name: 'Test', domain: 'test', email: 'info@example.com')

  end

  config.before(:each) do

    # Start transaction for this test
    DatabaseCleaner.start

    # Switch into the default tenant
    Apartment::Tenant.switch! 'test'

    # Use Timecop to freeze times on time-critical tests
    Timecop.return

  end

  config.after(:each) do

    # Reset tentant back to `public`
    Apartment::Tenant.reset

    # Rollback transaction
    DatabaseCleaner.clean

  end

end

这是我的测试(/spec/features/login_spec.rb):

require 'spec_helper'

describe 'the signin process', type: :feature do
  before :each do
    FactoryGirl.build(:user, email: 'user@example.com', password: 'password')
  end

  it 'signs me in' do
    visit '/users/sign_in'
    within('#session') do
      fill_in 'Email', :with => 'user@example.com'
      fill_in 'Password', :with => 'password'
    end
    click_button 'Sign in'
    expect(page).to have_content 'Success'
  end
end

1 个答案:

答案 0 :(得分:2)

如果您使用的是子域名,则可能需要设置如下内容:

<强>规格/支持/ subdomains.rb

def switch_to_subdomain(subdomain)
  # lvh.me always resolves to 127.0.0.1
  Capybara.app_host = "http://#{subdomain}.lvh.me"
end

def switch_to_main_domain
  Capybara.app_host = "http://lvh.me"
end

以上摘自this handy blog post。还有其他一些想法,重新:不依赖于lvh.me。

设置完成后,您可以使用url helper为此测试指定子域:

it 'signs me in' do
  switch_to_subdomain('test')
  visit new_user_session_path
  ...