0

我即将为 rspec 编写一个自定义格式化程序,我需要从格式化程序挂钩中访问 letDefinitions。

鉴于:

describe "Example" do
  let(:something) { "foo" }

  context "something to test" do
    # .....   
  end

end

在我的格式化程序中:

class MyFormatter
  RSpec::Core::Formatters.register self, :example_started

  def example_started(notification)
    @output << "Output of #{value_of_let(:something,notification)}"
  end

  private
  def value_of_let(what, notification)
    ### is there a way to find :something / 'foo' in notification?
  end
end
4

1 回答 1

0

LetDefinitions 不能在运行规范的命名空间中直接访问。同时,我从上面的例子中弄清楚了如何完成value_of_let方法:

def value_of_let(what, notification)
   begin
     something  = notification.example.example_group_instance.send(what)
     something # => "foo"
   rescue
     # something is not defined for this example
     nil
   end
end
于 2014-08-12T05:25:02.190 回答