RSpec的功能测试中,没有匹配'/users/sign_in'的路由

3

我正在尝试使用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

你运行过 rake db:test:prepare 命令了吗? - mr. Holiday
1个回答

2
如果您正在使用子域名,您可能需要设置以下内容:
spec/support/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

以上内容摘自这篇有用的博客文章。里面还有一些其他想法,关于不依赖于lvh.me的问题。
设置完成后,您可以使用url helper指定子域名进行测试。
it 'signs me in' do
  switch_to_subdomain('test')
  visit new_user_session_path
  ...

它能与公寓宝石协调吗?我的应用程序为每个租户使用子域,例如:company.example.com/users/sign_in - Slevin
谢谢!运行得很好 :) - Slevin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接