【问题标题】:Rspec multiple controllers with one common authenticationRspec 使用一种通用身份验证的多个控制器
【发布时间】:2017-10-26 04:42:03
【问题描述】:

大家好,我在 spec/controller 文件夹内的不同文件中对 API 进行了很多控制器测试。对于这些规格中的每一个,我都有一个

before do
  authenticate_user(user, role)
end 

这是规范的耗时,因为我必须为每个测试创建一个用户和一个角色。我想在上层有这个相同的助手,以便在相同的身份验证下执行我的所有控制器测试。

有人知道谁来实现这个目标吗?

存根这也是一种选择,但在用户之下有很多逻辑可能既困难又耗时。

谢谢

【问题讨论】:

    标签: ruby-on-rails ruby rspec


    【解决方案1】:

    您可以使用before(:suite)before(:all) 来防止多次调用此方法,例如

    before(:suite) do
      authenticate_user(user, role)
    end 
    

    您也可以在spec/support/sign_in_helper.rb 找到它,例如:

    module SignInHelper
      def sign_in
        before(:suite) do
          authenticate_user(user, role)
        end 
      end
    end
    

    并在spec_helper 使用它,例如:

    require 'spec/support/sign_in_helper'
    extend SignInHelper
    

    之后,您将能够像在顶层控制器规范中使用宏一样使用它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-24
      相关资源
      最近更新 更多