Rspec:一种将佣工包含在不同规范中的方法

时间:2016-02-19 08:57:50

标签: ruby-on-rails ruby rspec

letlet!提供了有用的功能。有时可能需要在不同的规范(DRY)中包含相同的帮助程序。例如,一组let以某种方式包含在每个请求规范中。有没有办法做这样的事情?

1 个答案:

答案 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
相关问题