Rails 教程9 练习 6:预期响应为 <redirect>,但为 <200> [英] Rails Tutorial Ch. 9 Exercise 6: Expected response to be a <redirect>, but was <200>
问题描述
我正在尝试编写测试和应用程序代码,以便在已登录的用户尝试创建用户或访问新用户路径时将其重定向到 root_path.
I am trying to write tests and application code to redirect users who are already signed-in to the root_path if they try to CREATE a user or visit the NEW user path.
以下是我在 user_pages_spec.rb 中编写的测试:
Here are the tests I have written in user_pages_spec.rb:
describe "for signed in users" do
let(:user) { FactoryGirl.create(:user) }
before { sign_in user }
describe "using a 'new' action" do
before { get new_user_path }
specify { response.should redirect_to(root_path) }
end
describe "using a 'create' action" do
before { post users_path }
specify { response.should redirect_to(root_path) }
end
end
用户控制器:
class UsersController < ApplicationController
before_action :unsigned_in_user, only: [:create, :new]
def new
@user = User.new
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
private
# Before filters
def user_params
params.require(:user).permit(:name, :email, :password,
:password_confirmation)
end
def unsigned_in_user
puts signed_in?
redirect_to root_url, notice: "You are already signed in." unless !signed_in?
end
end
puts signed_in?
返回 false.我假设这是问题所在,因为我希望它返回 true.以下是使用 rspec 运行测试后的错误.任何帮助表示赞赏.
The puts signed_in?
returns false. I am assuming this is the problem because I would expect it to return true. Here are the errors after running the tests using rspec. Any help is appreciated.
Failures:
1) User pages for signed in users using a 'create' action
Failure/Error: before { post users_path }
ActionController::ParameterMissing:
param not found: user
# ./app/controllers/users_controller.rb:52:in `user_params'
# ./app/controllers/users_controller.rb:20:in `create'
# ./spec/requests/user_pages_spec.rb:162:in `block (4 levels) in <top (required)>'
2) User pages for signed in users using a 'new' action
Failure/Error: specify { response.should redirect_to(root_path) }
Expected response to be a <redirect>, but was <200>
# ./spec/requests/user_pages_spec.rb:158:in `block (4 levels) in <top (required)>'
在 session_helper.rb 文件中:
Within the sessions_helper.rb file:
def signed_in?
!current_user.nil?
end
在规范/support/utilities.rb:
In 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.encrypt(remember_token))
else
visit signin_path
fill_in "Email", with: user.email
fill_in "Password", with: user.password
click_button "Sign in"
end
end
推荐答案
您是否能够通过测试?
如果你不是,我今天遇到了和你一样的问题,并且能够通过对测试进行两次更改来使测试通过 - 在发布时传递 user
哈希,并在 sign_in
方法上使用 no_capybara
选项,因为 get
和 post
不是水豚方法,我认为 RSpec如果我们在同一个测试中从水豚方法切换到非水豚方法,则行为不会像我们预期的那样.
In case you weren't, I had the same problem as you today, and was able to get the tests to pass by making two changes to the tests - passing a user
hash when POSTing, and using the no_capybara
option on the sign_in
method, since get
and post
are not capybara methods and I think RSpec doesn't behave as we might expect if we switch from capybara to non-capybara methods within the same test.
describe "for signed-in users" do
let(:user) { FactoryGirl.create(:user) }
before { sign_in user, no_capybara: true }
describe "using a 'new' action" do
before { get new_user_path }
specify { response.should redirect_to(root_path) }
end
describe "using a 'create' action" do
before do
@user_new = {name: "Example User",
email: "user@example.com",
password: "foobar",
password_confirmation: "foobar"}
post users_path, user: @user_new
end
specify { response.should redirect_to(root_path) }
end
end
这篇关于Rails 教程9 练习 6:预期响应为 <redirect>,但为 <200>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!