gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 05:24:42 26 4
gpt4 key购买 nike

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

最佳答案

如果您使用子域,您可能需要像这样设置:

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

以上摘自this handy blog post .里面还有一些其他的想法,re: 不依赖 lvh.me。

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

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

关于ruby-on-rails - 在使用 RSpec 进行功能测试时,没有路由匹配 '/users/sign_in',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33997804/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com