我有一個幫助程序模塊,include
'd在RSpec.describe
塊中。這裏的大多數方法都與管理登錄/註銷狀態有關,所以我想確保使用該幫助程序的任何內容都會自動清除全局狀態,以避免泄漏雙/嘲。在rspec幫助程序模塊中添加'after'塊
但我看到in the docs每一個例子似乎是有關添加只是輔助方法,而不是額外的before
/after
塊。
我可以添加一個額外的after
塊,它不會覆蓋輔助模塊中現有的塊嗎?
我有一個幫助程序模塊,include
'd在RSpec.describe
塊中。這裏的大多數方法都與管理登錄/註銷狀態有關,所以我想確保使用該幫助程序的任何內容都會自動清除全局狀態,以避免泄漏雙/嘲。在rspec幫助程序模塊中添加'after'塊
但我看到in the docs每一個例子似乎是有關添加只是輔助方法,而不是額外的before
/after
塊。
我可以添加一個額外的after
塊,它不會覆蓋輔助模塊中現有的塊嗎?
如果我正確理解你的問題,像這樣的事情應該做的伎倆。 (實施例是與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
如果我誤解了這個問題,請告訴我,我可以進一步澄清或更改示例
答案是是。在您的測試中創建的每個before | after
塊將在塊之前運行幫助文件。
所以你塊不會覆蓋幫手塊。