Michael Hartl Chapter9(授权)错误?

Jaz*_*hir 1 ruby ruby-on-rails ruby-on-rails-3 ruby-on-rails-4

身份验证测试生成错误,我无法理解.它说单元测试断言错误.

Authentication authorization for non-signed-in users in the Users controller submitting to the update action 
     Failure/Error: specify { expect(response).to redirect_to(signin_path) }
     NoMethodError:
       undefined method `assertions' for #<RSpec::Rails::TestUnitAssertionAdapter::AssertionDelegator:0xba527604>
Run Code Online (Sandbox Code Playgroud)

authentication_pages_spec.rb

 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
Run Code Online (Sandbox Code Playgroud)

user_controller.rb

授权应用程序代码使用before过滤器,该过滤器使用before_action命令来安排在给定操作之前调用的特定方法.(之前过滤器的命令曾被称为before_filter,但Rails核心团队决定重命名它以强调过滤器在特定控制器操作之前发生.)要求用户登录,我们定义了signed_in_user方法并调用它使用before_action:signed_in_user,如图所示

class UsersController < ApplicationController
  before_action :signed_in_user, only: [:edit, :update]
  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
Run Code Online (Sandbox Code Playgroud)

Abh*_*nay 6

尝试将以下行添加到您的gem文件中,并从gem文件中删除旧的"rspec-rails"行.

gem "rspec-rails", '~> 2.14.0.rc1'
Run Code Online (Sandbox Code Playgroud)

然后运行这些命令,

$ bundle update

$ bundle install
Run Code Online (Sandbox Code Playgroud)

然后再次检查测试.