【问题标题】:Adding `after` block in rspec helper module在 rspec 帮助器模块中添加 `after` 块
【发布时间】:2017-07-23 21:28:32
【问题描述】:

我在RSpec.describe 块中有一个include'd 助手模块。那里的大多数方法都与管理登录/注销状态有关,所以我想确保使用该帮助程序的任何东西都会自动清理全局状态以避免泄漏双打/模拟。

但我看到的每个示例 in the docs 似乎都只是添加了辅助方法,而不是额外的 before / after 块。

我可以添加一个额外的after 块,它不会覆盖帮助模块中的现有块吗?

【问题讨论】:

    标签: ruby rspec


    【解决方案1】:

    如果我正确理解了您的问题,那么这样的事情应该可以解决问题。 (例如,您的链接上的第一个添加了 after 块。)

    helpers.rb

    module Helpers
      def help
        :available
      end
    
      def self.included(base)
        base.after(:each) do
          puts "after in helpers.rb"
        end
      end
    end
    

    example.rb

    require './helpers'
    RSpec.configure do |c|
      c.include Helpers
    end
    
    RSpec.describe "an example group" do
      after(:each) do
        puts "After in example.rb"
      end
    
      it "has access to the helper methods defined in the module" do
        expect(help).to be(:available)
      end
    end
    

    然后运行它

    $ rspec example.rb
    # After in example.rb
    # after in helpers.rb
    # .
    # Finished in 0.00196 seconds (files took 0.07939 seconds to load)
    # 1 example, 0 failures
    

    如果我误解了问题,请告诉我,我可以进一步澄清或更改示例

    【讨论】:

      【解决方案2】:

      答案是是的。在您的测试中创建的每个 before | after 块都将在 Helper 文件上的 before | after 块之前运行。

      因此,您的 blocks 不会覆盖 helper 块。

      【讨论】:

        猜你喜欢
        • 2011-07-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-24
        • 1970-01-01
        • 1970-01-01
        • 2020-10-25
        • 2016-10-13
        相关资源
        最近更新 更多