let
和let!
提供了有用的功能。有时可能需要在不同的规范(DRY)中包含相同的帮助程序。例如,一组let以某种方式包含在每个请求规范中。有没有办法做这样的事情?
答案 0 :(得分:2)
您可以定义shared_context
:
# shared_stuff.rb
shared_context "shared stuff" do
let(:shared_let) { "foo" }
end
并在各种示例中使用它:
# example.rb
require_relative "shared_stuff.rb"
describe "an example" do
include_context "shared stuff"
it "has access to shared lets" do
expect(shared_let).to eq("foo")
end
end
输出:
$ rspec example.rb
an example
has access to shared lets
Finished in 0.00076 seconds (files took 0.11233 seconds to load)
1 example, 0 failures