【问题标题】:Rspec let scoping with shared examplesRspec 让范围与共享示例
【发布时间】:2014-12-22 10:23:48
【问题描述】:

我对 let 和 shared 示例有疑问。发生的事情是我的第二个 shared_context 覆盖了第一个。

例子:

RSpec.shared_examples "an example" do
  include_context "a"
  include_context "b"
end

shared_context 'a' do
  let(:let_example) { p 'let_example a' }

  include_context "c"
end

shared_context 'b' do
  let(:let_example) { p 'let_example b' }

  include_context "c"
end

shared_context 'c' do
  before do
    let_example
  end
end

它总是打印let_example b

【问题讨论】:

  • 您正在重新分配变量,那么您还能期待什么?
  • @Anthony 我只是不想重复自己。我有类似的测试只改变一个变量,这就是我在更高级别的上下文中使用 let 的原因。为我工作将include_context "a"include_context "b" 分别放在一个上下文中。但不知道这样做是否正确。

标签: ruby-on-rails ruby rspec rspec-rails rspec3


【解决方案1】:

在您的示例方法中,已经完成了重新定义。当include_context 'a' 被执行时,它定义let_example 作为打印'let_example a' 的方法。但是当你include_context 'b' 被执行时,重新定义方法let_example,它会有效地打印let_example 'b'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-17
    • 2017-01-30
    • 1970-01-01
    相关资源
    最近更新 更多