Michael Hartl Chapter9(授权)错误?

时间:2014-07-17 10:42:23

标签: ruby-on-rails ruby 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>

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

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

1 个答案:

答案 0 :(得分:6)

尝试将以下这一行添加到您的gem文件中,然后移除旧的&#34; rspec-rails&#34;你宝石文件中的一行。

gem "rspec-rails", '~> 2.14.0.rc1'

然后运行这些命令,

$ bundle update

$ bundle install
然后再次检查测试。