hartl教程在9.2.1结束时失败测试

时间:2013-08-05 13:39:32

标签: ruby-on-rails-4 railstutorial.org

我正在研究Hartl的rails教程,第9.2.1节末尾。我的测试没有通过。无法弄清楚为什么。希望有人可以提供帮助。我附加了错误消息和两个相关文件1)authentication_pages_spec.rb和2)users_controller.rb

故障:

1)访问编辑页面的Users控制器中的非登录用户的身份验证授权      失败/错误:它{should have_title('登录')}        预期#has_title?(“登录”)返回true,得到错误      './spec/requests/authentication_pages_spec.rb:57:在'

中的块(6级)

2)用户控制器中提交更新操作的非登录用户的身份验证授权      失败/错误:在{patch user_path(user)}之前      ActionController的:: ParameterMissing:        未找到参数:用户      #./app/controllers/users_controller.rb:40:in user_params' # ./app/controllers/users_controller.rb:27:in更新'      './spec/requests/authentication_pages_spec.rb:61:in'块(6级)in'

在2.08秒完成 60个例子,2个失败

失败的例子:

rspec ./spec/requests/authentication_pages_spec.rb:57#访问编辑页面的Users控制器中未登录用户的身份验证授权 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

1 个答案:

答案 0 :(得分:4)

您在users_controller.rb的第2行上缺少对signed_in_user过滤器的调用。您在控制器的末尾定义了过滤器,但您没有调用它! :)

在本教程的最后,该行应如下所示:

before_action :signed_in_user, only: [:index, :edit, :update, :destroy, :following, :followers]

但是,既然你还没有创建粉丝,我认为应该是这样的:

before_action :signed_in_user, only: [:index, :edit, :update, :destroy]

我发现在我的硬盘驱动器上复制代码的最终源代码非常有用,因此每当我在执行本书时遇到错误,我都可以将我的代码与原始代码进行比较。