正如标题所说,我正在关注Michael Hartl的RoR Book,我目前正在进行第9.3章 - 显示用户。所有测试都在本章之前通过, 我正在阅读本书的Rails 4版本,我对这个RoR很新。
我确实捆绑了exec rspec spec /并得到了这个错误
1)用户中未登录用户的身份验证授权 控制器访问用户索引 失败/错误:它{should have_title('登录')} 预期#has_title?(“登录”)返回true,得到错误 './spec/requests/authentication_pages_spec.rb:80:in'块(6级)in'
2)用户页面索引 失败/错误:访问users_path ::的ActionView ::模板错误: 错误的参数数量(2为1) #./app/helpers/users_helper.rb:4:in
gravatar_for' # ./app/views/users/index.html.erb:7:in
阻止_app_views_users_index_html_erb__3697129218785207645_26541860' './app/views/users/index.html.erb:5:in_app_views_users_index_html_erb__3697129218785207645_26541860' # ./spec/requests/user_pages_spec.rb:12:in
阻止(3级)'3)用户页面索引 失败/错误:访问users_path ::的ActionView ::模板错误: 错误的参数数量(2为1) #./app/helpers/users_helper.rb:4:in
gravatar_for' # ./app/views/users/index.html.erb:7:in
阻止_app_views_users_index_html_erb__3697129218785207645_26541860' './app/views/users/index.html.erb:5:in_app_views_users_index_html_erb__3697129218785207645_26541860' # ./spec/requests/user_pages_spec.rb:12:in
阻止(3级)'4)用户页面索引应列出每个用户 失败/错误:访问users_path ::的ActionView ::模板错误: 错误的参数数量(2为1) #./app/helpers/users_helper.rb:4:in
gravatar_for' # ./app/views/users/index.html.erb:7:in
阻止_app_views_users_index_html_erb__3697129218785207645_26541860' './app/views/users/index.html.erb:5:in_app_views_users_index_html_erb__3697129218785207645_26541860' # ./spec/requests/user_pages_spec.rb:12:in
阻止(3级)'在3.16秒完成66例,4次失败
失败的例子:
rspec ./spec/requests/authentication_pages_spec.rb:80#身份验证 在Users控制器访问中对非登录用户的授权 用户索引rspec ./spec/requests/user_pages_spec.rb:16#User pages index rspec ./spec/requests/user_pages_spec.rb:15#用户页面 index rspec ./spec/requests/user_pages_spec.rb:18#用户页面索引 应该列出每个用户
用种子40709随机化
这是我的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('Users', href: users_path) }
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 "when attempting to visit a protected page" do
before do
visit edit_user_path(user)
fill_in "Email", with: user.email
fill_in "Password", with: user.password
click_button "Sign in"
end
describe "after signing in" do
it "should render the desired protected page" do
expect(page).to have_title('Edit user')
end
end
end
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
describe "visiting the user index" do
before { visit users_path }
it { should have_title('Sign in') }
end
end
end
describe "as wrong user" do
let(:user) { FactoryGirl.create(:user) }
let(:wrong_user) { FactoryGirl.create(:user, email: "wrong@example.com") }
before { sign_in user, no_capybara: true }
describe "submitting a GET request to the Users#edit action" do
before { get edit_user_path(wrong_user) }
specify { expect(response.body).not_to match(full_title('Edit user')) }
specify { expect(response).to redirect_to(root_url) }
end
describe "submitting a PATCH request to the Users#update action" do
before { patch user_path(wrong_user) }
specify { expect(response).to redirect_to(root_url) }
end
end
end
end
这是我的users_controller.rb
class UsersController < ApplicationController
before_action :signed_in_user, only: [:edit, :update]
before_action :correct_user, only: [:edit, :update]
def index
@users = User.all
end
def show
@user = User.find(params[:id])
end
def new
@user = User.new
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"
redirect_to @user
else
render 'edit'
end
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
def user_params
params.require(:user).permit(:name, :email, :password,
:password_confirmation)
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
end
这是我的user_pages_spec.rb
class UsersController < ApplicationController
before_action :signed_in_user, only: [:edit, :update]
before_action :correct_user, only: [:edit, :update]
def index
end
def show
@user = User.find(params[:id])
end
def new
@user = User.new
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"
redirect_to @user
else
render 'edit'
end
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
def user_params
params.require(:user).permit(:name, :email, :password,
:password_confirmation)
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
end
我真的不知道出了什么问题,请指出正确的方向。
答案 0 :(得分:0)
这看起来像是模板错误。
一个错误指向此文件:/app/helpers/users_helper.rb第4行,看起来gravatar_for
中的参数数量错误。接下来要看的是/app/views/users/index.html.erb第7行。
答案 1 :(得分:0)
我明白了。
问题在于gravatar_for参数。
我犯了错误,并且没有更新users_helper中的gravatar_for方法,这就是提供此错误的内容。
def gravatar_for(user, options = { size: 50 })
gravatar_id = Digest::MD5::hexdigest(user.email.downcase)
size = options[:size]
gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}?s=#{size}"
image_tag(gravatar_url, alt: user.name, class: "gravatar")
end
更新了gravatar_for方法。