Ruby on rails 用户登录后,为什么要设计重定向到视图中的Sign_而不是指定的路径?
我对设计重定向有问题。默认情况下,在用户登录并注册后,它将重定向到toroot_路径。我想将该路径更改为播放列表路径 每次我运行测试时,它都会失败,并转到路径中的用户/签名处,我不知道为什么。我已经厌倦了在Stackoverflow和Github上找到的不同解决方案,但没有任何效果。可能是解决方案很明显,我看不到,或者我的测试写得不好。奇怪的是,注册规范通过了,但是每次注册规范都失败了。我需要另一只眼睛和帮助 更新*** 基于Sevensacat的评论,我更新了下面的测试,但仍然得到相同的结果 路线 会话新 spec/factories/users.rb 规格/特性/注册规格。rb此规格良好且合格Ruby on rails 用户登录后,为什么要设计重定向到视图中的Sign_而不是指定的路径?,ruby-on-rails,rspec,devise,Ruby On Rails,Rspec,Devise,我对设计重定向有问题。默认情况下,在用户登录并注册后,它将重定向到toroot_路径。我想将该路径更改为播放列表路径 每次我运行测试时,它都会失败,并转到路径中的用户/签名处,我不知道为什么。我已经厌倦了在Stackoverflow和Github上找到的不同解决方案,但没有任何效果。可能是解决方案很明显,我看不到,或者我的测试写得不好。奇怪的是,注册规范通过了,但是每次注册规范都失败了。我需要另一只眼睛和帮助 更新*** 基于Sevensacat的评论,我更新了下面的测试,但仍然得到相同的结果
require 'spec_helper'
feature "Sign up as a user" do
scenario "with an email address" do
user = FactoryGirl.build(:user)
visit root_path
click_link "Create Account"
fill_in "Email", with: user.email
fill_in "Password", with: user.password
fill_in "Password confirmation", with: user.password_confirmation
click_button "Sign up"
expect(current_path).to eq playlists_path
expect(page).to have_css '.alert', text: 'Welcome! You have signed up successfully.'
expect(page).to have_link 'Sign out', href: destroy_user_session_path
expect(page).to have_link 'Edit profile', href: edit_user_registration_path
end
end
spec/features/sign_in_spec.rb此规范不好且失败
require 'spec_helper'
feature "Sign in as a user" do
scenario "with an email address and password" do
user = FactoryGirl.create(:user)
visit root_path
click_link "Sign in"
fill_in "Email", with: user.email
fill_in "Password", with: user.password
click_button "Sign in"
expect(current_path).to eq playlists_path
expect(page).to have_css '.alert', text: 'Signed in successfully.'
expect(page).to have_link 'Sign out', href: destroy_user_session_path
expect(page).to have_link 'Edit profile', href: edit_user_registration_path
end
end
当我在_spec.rb中运行rspec spec/features/sign_时,它失败:
Failures:
1) Sign in as a user with an email address and password
Failure/Error: expect(current_path).to eq playlists_path
expected: "/playlists"
got: "/users/sign_in"
(compared using ==)
# ./spec/features/signin_spec.rb:11:in `block (2 levels) in <top (required)>'
奇怪的是,虽然我的sign_in_规范失败了,但当我运行应用程序时,它运行良好,并且在登录和注册后重定向到播放列表路径
为什么要设计这种奇怪的重定向,或者为什么我的测试失败?登录规范中是否存在该用户?很可能,您需要在该规范的设置中使用该电子邮件和密码创建用户
规范通常单独运行,并且在测试之间删除所有数据-如果在运行规范时依赖数据,则需要在规范中进行设置。执行以下操作:
bundle exec rake db:drop RAILS_ENV=test
bundle exec rake db:create RAILS_ENV=test
bundle exec rake db:schema:load RAILS_ENV=test
改变
user=FactoryGirl.create:user
到
user=FactoryGirl.build:user
我的考试通过了
FactoryGirl.define do
sequence(:email) { |n| "someone#{n}@example.com" }
factory :user do
email
password 'password'
password_confirmation { |u| u.password }
end
end
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
# require 'rspec/autorun'
require 'capybara/rspec'
require 'database_cleaner'
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)
RSpec.configure do |config|
config.include Capybara::DSL
config.include FactoryGirl::Syntax::Methods
config.include Devise::TestHelpers, type: :controller
config.infer_base_class_for_anonymous_controllers = false
config.order = "random"
config.before(:suite) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end
require 'spec_helper'
feature "Sign up as a user" do
scenario "with an email address" do
user = FactoryGirl.build(:user)
visit root_path
click_link "Create Account"
fill_in "Email", with: user.email
fill_in "Password", with: user.password
fill_in "Password confirmation", with: user.password_confirmation
click_button "Sign up"
expect(current_path).to eq playlists_path
expect(page).to have_css '.alert', text: 'Welcome! You have signed up successfully.'
expect(page).to have_link 'Sign out', href: destroy_user_session_path
expect(page).to have_link 'Edit profile', href: edit_user_registration_path
end
end
require 'spec_helper'
feature "Sign in as a user" do
scenario "with an email address and password" do
user = FactoryGirl.create(:user)
visit root_path
click_link "Sign in"
fill_in "Email", with: user.email
fill_in "Password", with: user.password
click_button "Sign in"
expect(current_path).to eq playlists_path
expect(page).to have_css '.alert', text: 'Signed in successfully.'
expect(page).to have_link 'Sign out', href: destroy_user_session_path
expect(page).to have_link 'Edit profile', href: edit_user_registration_path
end
end
Failures:
1) Sign in as a user with an email address and password
Failure/Error: expect(current_path).to eq playlists_path
expected: "/playlists"
got: "/users/sign_in"
(compared using ==)
# ./spec/features/signin_spec.rb:11:in `block (2 levels) in <top (required)>'
bundle exec rake db:drop RAILS_ENV=test
bundle exec rake db:create RAILS_ENV=test
bundle exec rake db:schema:load RAILS_ENV=test