我正在研究 Hartl 的 Rails 教程,第 9.2.1 节结束。我的测试没有通过。想不通为什么。希望有人可以提供帮助。我附上了错误消息和两个相关文件 1) authentication_pages_spec.rb 和 2) users_controller.rb 下面
失败:
1)访问编辑页面的用户控制器中未登录用户的身份验证授权失败/错误:它{应该有_title('登录')}预期#has_title?(“登录”)返回真,得到假# ./spec/requests/authentication_pages_spec.rb:57:in `block (6 levels) in '
2)用户控制器中的未登录用户的身份验证授权提交更新操作失败/错误:在 { patch user_path(user) } ActionController::ParameterMissing: param not found: user # ./app/controllers/users_controller 之前.rb:40:in user_params'
# ./app/controllers/users_controller.rb:27:in
update' # ./spec/requests/authentication_pages_spec.rb:61:in `block (6 levels) in '
在 2.08 秒内完成 60 个示例,2 次失败
失败的例子:
rspec ./spec/requests/authentication_pages_spec.rb:57 # 访问编辑页面的用户控制器中未登录用户的身份验证授权 rspec ./spec/requests/authentication_pages_spec.rb:62 # 未签名的身份验证授权-在用户控制器中的用户中提交更新操作
我的 authentication_pages_spec.rb 是
require 'spec_helper'
describe "Authentication" do
subject { page }
describe "signin page" do
before { visit signin_path }
it { should have_content('Sign in') }
it { should have_title('Sign in') }
end
describe "signin" do
before { visit signin_path }
describe "with invalid information" do
before { click_button "Sign in" }
it { should have_title('Sign in') }
it { should have_selector('div.alert.alert-error', text: 'Invalid') }
describe "after visiting another page" do
before { click_link "Home" }
it { should_not have_selector('div.alert.alert-error') }
end
end
describe "with valid information" do
let(:user) { FactoryGirl.create(:user) }
before { sign_in user }
it { should have_title(user.name) }
it { should have_link('Profile', href: user_path(user)) }
it { should have_link('Settings', href: edit_user_path(user)) }
it { should have_link('Sign out', href: signout_path) }
it { should_not have_link('Sign in', href: signin_path) }
describe "followed by signout" do
before { click_link "Sign out" }
it { should have_link('Sign in') }
end
end
end
describe "authorization" do
describe "for non-signed-in users" do
let(:user) { FactoryGirl.create(:user) }
describe "in the Users controller" do
describe "visiting the edit page" do
before { visit edit_user_path(user) }
it { should have_title('Sign in') }
end
describe "submitting to the update action" do
before { patch user_path(user) }
specify { expect(response).to redirect_to(signin_path) }
end
end
end
end
end
users_controller.rb 是
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
end
def new
@user = User.new
end
def create
@user = User.new(user_params)
if @user.save
flash[:success] = "Welcome to the Sample App!"
redirect_to @user
else
render 'new'
end
end
def edit
@user = User.find(params[:id])
end
def update
@user = User.find(params[:id])
if @user.update_attributes(user_params)
flash[:success] = "Profile updated"
sign_in @user
redirect_to @user
else
render 'edit'
end
end
private
def user_params
params.require(:user).permit(:name, :email, :password,
:password_confirmation)
end
# Before filters
def signed_in_user
redirect_to signin_url, notice: "Please sign in." unless signed_in?
end
end