2013-12-23 39 views
0

正如标题所说,我正在关注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 

我真的不知道哪里出了问题,请指点我正确的方向。

回答

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方法。

0

这看起来像一个模板错误。

一个错误指向这个文件:/app/helpers/users_helper.rb第4行,看起来你在gravatar_for中有错误的参数个数。然后下一个地方是/app/views/users/index.html.erb第7行。

+0

这是我的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

    相关问题