有没有办法从rspec格式化程序钩子中访问LetDefinitions?

时间:2014-08-08 07:22:03

标签: ruby rspec rspec3

我即将为rspec编写自定义格式化程序,我需要从formatter-hooks中访问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

1 个答案:

答案 0 :(得分: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