正如標題所說,我正在關注Michael Hartl的RoR Book,我目前正在參閱第9.3章 - 顯示用戶。本章之前通過的所有測試, 我遵循本書的Rails 4版本,我對這個RoR很新。Hartl的RoR教程在第9.3章 - 顯示所有用戶中失敗測試
我做了捆綁的exec rspec的投機/和得到這個錯誤
1) Authentication authorization for non-signed-in users in the Users controller visiting the user index Failure/Error: it { should have_title('Sign in') } expected #has_title?("Sign in") to return true, got false # ./spec/requests/authentication_pages_spec.rb:80:in `block (6 levels) in '
2) User pages index Failure/Error: visit users_path ActionView::Template::Error: wrong number of arguments (2 for 1) # ./app/helpers/users_helper.rb:4:in
gravatar_for' # ./app/views/users/index.html.erb:7:in
block 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
block (3 levels) in '3) User pages index Failure/Error: visit users_path ActionView::Template::Error: wrong number of arguments (2 for 1) # ./app/helpers/users_helper.rb:4:in
gravatar_for' # ./app/views/users/index.html.erb:7:in
block 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
block (3 levels) in '4) User pages index should list each user Failure/Error: visit users_path ActionView::Template::Error: wrong number of arguments (2 for 1) # ./app/helpers/users_helper.rb:4:in
gravatar_for' # ./app/views/users/index.html.erb:7:in
block 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
block (3 levels) in 'Finished in 3.16 seconds 66 examples, 4 failures
Failed examples:
rspec ./spec/requests/authentication_pages_spec.rb:80 # Authentication authorization for non-signed-in users in the Users controller visiting the user index rspec ./spec/requests/user_pages_spec.rb:16 # User pages index rspec ./spec/requests/user_pages_spec.rb:15 # User pages index rspec ./spec/requests/user_pages_spec.rb:18 # User pages index should list each user
Randomized with seed 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: "[email protected]") }
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
我真的不知道哪裏出了問題,請指點我正確的方向。
這是我的User_helper.rb 模塊UsersHelper #返回給定用戶的Gravatar(http://gravatar.com/)。 def gravatar_for(user) gravatar_id = Digest :: MD5 :: hexdigest(user.email.downcase) gravatar_url =「https://secure.gravatar.com/avatar/#{gravatar_id}」 image_tag(gravatar_url,alt :user.name,類: 「的gravatar」) 端 端 和我的index.html <%提供(:標題, '所有用戶')%>
所有用戶
<%@ users.each do | user | %>- <%= gravatar_for用戶,尺寸:52%> <%=的link_to user.name,用戶%>
<% end %>
– RoobNoob