首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在使用RSpec进行特性测试时,没有匹配“/user/sign”的路由

在使用RSpec进行特性测试时,没有匹配“/user/sign”的路由
EN

Stack Overflow用户
提问于 2015-11-30 11:41:36
回答 1查看 566关注 0票数 3

我正在尝试用RSpec编写一个特性测试,以测试我的符号的形式,但我总是得到以下内容:

代码语言:javascript
复制
  1) the signin process signs me in
     Failure/Error: visit '/users/sign_in'
     ActionController::RoutingError:
       No route matches [GET] "/users/sign_in"

但这条路线存在:

代码语言:javascript
复制
               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

代码语言:javascript
复制
# 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):

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-11-30 11:46:23

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

spec/support/subdomains.rb

代码语言:javascript
复制
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

以上内容取自这篇便利的博客文章。还有一些其他的想法,re:不依赖lvh.me。

一旦设置好它,您就可以使用此测试的url助手指定子域:

代码语言:javascript
复制
it 'signs me in' do
  switch_to_subdomain('test')
  visit new_user_session_path
  ...
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33997804

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档