Ruby on rails Rails教程第9章:授权重定向测试失败
我已经浏览了几乎所有Rails教程第9章关于StackOverflow的问题。似乎没有人有同样的问题。也许这只是一个打字错误或是我错过的一些愚蠢的事情,我只是需要一些新鲜的眼睛。任何帮助都将不胜感激 错误:Ruby on rails Rails教程第9章:授权重定向测试失败,ruby-on-rails,ruby-on-rails-4,railstutorial.org,Ruby On Rails,Ruby On Rails 4,Railstutorial.org,我已经浏览了几乎所有Rails教程第9章关于StackOverflow的问题。似乎没有人有同样的问题。也许这只是一个打字错误或是我错过的一些愚蠢的事情,我只是需要一些新鲜的眼睛。任何帮助都将不胜感激 错误: Failures: 1) Authentication authorization as non-admin user submitting a DELETE request to the Users#destroy action Failure/Error: specify { exp
Failures:
1) Authentication authorization as non-admin user submitting a DELETE request to the Users#destroy action
Failure/Error: specify { expect(response).to redirect_to(root_url) }
Expected response to be a redirect to <http://www.example.com/> but was a redirect to <http://www.example.com/signin>.
Expected "http://www.example.com/" to be === "http://www.example.com/signin".
# ./spec/requests/authentication_pages_spec.rb:60:in `block (5 levels) in <top (required)>'
2) Authentication authorization as wrong user submitting a GET request to the Users#edit action
Failure/Error: specify { expect(response).to redirect_to(root_url) }
Expected response to be a redirect to <http://www.example.com/> but was a redirect to <http://www.example.com/signin>.
Expected "http://www.example.com/" to be === "http://www.example.com/signin".
# ./spec/requests/authentication_pages_spec.rb:108:in `block (5 levels) in <top (required)>'
3) Authentication authorization as wrong user submitting a PATCH request to the Users#update action
Failure/Error: specify { response.should redirect_to(root_url) }
Expected response to be a redirect to <http://www.example.com/> but was a redirect to <http://www.example.com/signin>.
Expected "http://www.example.com/" to be === "http://www.example.com/signin".
# ./spec/requests/authentication_pages_spec.rb:113:in `block (5 levels) in <top (required)>'
Failed examples:
rspec ./spec/requests/authentication_pages_spec.rb:60 # Authentication authorization as non-admin user submitting a DELETE request to the Users#destroy action
rspec ./spec/requests/authentication_pages_spec.rb:108 # Authentication authorization as wrong user submitting a GET request to the Users#edit action
rspec ./spec/requests/authentication_pages_spec.rb:113 # Authentication authorization as wrong user submitting a PATCH request to the Users#update action
用户控制器:
class UsersController < ApplicationController
before_action :signed_in_user, only: [:index, :edit, :update, :destroy]
before_action :correct_user, only: [:edit, :update]
before_action :admin_user, only: :destroy
before_action :signed_in_redirect, only: [:new, :create]
def destroy
User.find(params[:id]).destroy
flash[:success] = "User deleted."
redirect_to users_url
end
def index
@users = User.paginate(page: params[:page])
end
def new
@user = User.new
end
def show
@user = User.find(params[:id])
end
def create
@user = User.new(user_params)
if @user.save
sign_in @user
flash[:success] = "Welcome to the Sample App!"
redirect_to @user
else
render 'new'
end
end
def edit
end
def update
if @user.update_attributes(user_params)
flash[:success] = "Profile updated"
redirect_to @user
else
render 'edit'
end
end
private
def user_params
params.require(:user).permit(:name, :email, :password,
:password_confirmation, :admin)
end
# Before filters
def signed_in_user
unless signed_in?
store_location
redirect_to signin_url, notice: "Please sign in."
end
end
def correct_user
@user = User.find(params[:id])
redirect_to(root_url) unless current_user?(@user)
end
def admin_user
redirect_to(root_url) unless current_user.admin?
end
def signed_in_redirect
redirect_to root_url, note: "Already signed in" if signed_in?
end
end
-编辑2-
utilities.rb:
include ApplicationHelper
def sign_in(user, options={})
if options[:no_capybara]
# Sign in when not using Capybara.
remember_token = User.new_remember_token
cookies[:remember_token] = remember_token
user.update_attribute(:remember_token, User.hash(remember_token))
else
visit signin_path
fill_in "Email", with: user.email
fill_in "Password", with: user.password
click_button "Sign in"
end
end
def valid_signin(user)
fill_in "Email", with: user.email
fill_in "Password", with: user.password
click_button "Sign in"
end
RSpec::Matchers.define :have_error_message do |message|
match do |page|
expect(page).to have_selector('div.alert.alert-error', text: message)
end
end
-编辑3-
spec/support/utilities.rb的编辑部分
def sign_in(user, options={})
if options[:no_capybara]
# Sign in when not using Capybara.
remember_token = User.new_remember_token
cookies[:remember_token] = remember_token
user.update_attribute(:remember_token, User.hash(remember_token))
puts user
else
visit signin_path
fill_in "Email", with: user.email
fill_in "Password", with: user.password
click_button "Sign in"
end
end
app/controllers/users\u controller的编辑部分
def signed_in_user
puts current_user
unless signed_in?
store_location
redirect_to signin_url, notice: "Please sign in."
end
end
rspec spec/
╭─von at Vons-Mac in ~/dev/web/rails/sample_app on updating-users✘✘✘ using ‹ruby-2.1.0@railstutorial_4_0› 14-05-15 - 19:33:59
╰─○ rspec spec/
..
F
F
F
.
#<User:0x00000106b79240>
.
.
.
...................................................#<User:0x000001026f1370>
.#<User:0x00000106361850>
.#<User:0x00000106d7b688>
.#<User:0x0000010267df38>
#<User:0x00000106a77220>
.#<User:0x00000107abc9f0>
#<User:0x0000010607c748>
.#<User:0x0000010686cd68>
#<User:0x00000101610ec0>
#<User:0x000001036d47b0>
#<User:0x00000101e54730>
.#<User:0x00000107bdc858>
.#<User:0x0000010244a590>
.........#<User:0x00000106b81008>
.#<User:0x00000106331a60>
.#<User:0x0000010629b330>
.#<User:0x0000010a96cae8>
#<User:0x0000010a092b48>
.#<User:0x0000010a9f8160>
#<User:0x0000010a09b1f8>
.#<User:0x0000010a0f84e8>
#<User:0x0000010aa6f580>
.#<User:0x000001064eef88>
#<User:0x0000010ab82080>
.#<User:0x00000106535320>
#<User:0x000001065c83c8>
.#<User:0x0000010665a1d8>
#<User:0x0000010a17d300>
.
╭─von在Vons Mac上~/dev/web/rails/sample_应用程序中更新用户✘✘✘ 使用èruby-2.1。0@railstutorial_4_0› 14-05-15 - 19:33:59
╰─○ rspec规范/
..
F
F
F
.
#
.
.
.
...................................................#
.#
.#
.#
#
.#
#
.#
#
#
#
.#
.#
.........#
.#
.#
.#
#
.#
#
.#
#
.#
#
.#
#
.#
#
.
之前的错误基本上是说用户没有在应该登录的时候登录。不确定5年后这是否有用。但我在一两年前意识到问题在于:我在返回整个用户记录,而我本应该返回一个字段。因此,失败的测试和由此产生的错误。常见的因素似乎是你的符号在方法中;我猜这不起作用,这就是为什么这些操作将转到登录页面而不是根。i、 e.用户在执行您正在测试的操作之前没有实际登录。你能分享吗?好的。添加了sessions\u helper,其中包含sign\u in
方法。您是否在specs/spec\u helper.rb
或specs/support/
中定义了sign\u in
方法?如果是的话,你能提供代码吗?当然可以。我添加了另一个编辑,包括spec/support/utilities.rb中的内容,感谢这两个更新;我建议您在会话助手的signed\u-in?
和current\u-user
方法中以及使用的sign\u-in
方法中进行一些调试。从错误中,我认为您会发现signed\u in?
返回false,因为当前用户
无法从cookie中找到用户,可能是因为sign\u in
未正确生成和存储cookie。
def signed_in_user
puts current_user
unless signed_in?
store_location
redirect_to signin_url, notice: "Please sign in."
end
end
╭─von at Vons-Mac in ~/dev/web/rails/sample_app on updating-users✘✘✘ using ‹ruby-2.1.0@railstutorial_4_0› 14-05-15 - 19:33:59
╰─○ rspec spec/
..
F
F
F
.
#<User:0x00000106b79240>
.
.
.
...................................................#<User:0x000001026f1370>
.#<User:0x00000106361850>
.#<User:0x00000106d7b688>
.#<User:0x0000010267df38>
#<User:0x00000106a77220>
.#<User:0x00000107abc9f0>
#<User:0x0000010607c748>
.#<User:0x0000010686cd68>
#<User:0x00000101610ec0>
#<User:0x000001036d47b0>
#<User:0x00000101e54730>
.#<User:0x00000107bdc858>
.#<User:0x0000010244a590>
.........#<User:0x00000106b81008>
.#<User:0x00000106331a60>
.#<User:0x0000010629b330>
.#<User:0x0000010a96cae8>
#<User:0x0000010a092b48>
.#<User:0x0000010a9f8160>
#<User:0x0000010a09b1f8>
.#<User:0x0000010a0f84e8>
#<User:0x0000010aa6f580>
.#<User:0x000001064eef88>
#<User:0x0000010ab82080>
.#<User:0x00000106535320>
#<User:0x000001065c83c8>
.#<User:0x0000010665a1d8>
#<User:0x0000010a17d300>
.