2012-05-04 39 views
3

这是完全愚蠢和不重要的,但我只是好奇:使用RSpec,我可以以某种方式访问​​我在范围内的“深度”?这是...有没有办法在RSpec中获取当前的作用域级别?

describe SomeClass do 
    describe "#some_method" do 
    context "within some context" do 
     it "is possible, what I'm asking" do 
     # Actually, I'm not entirely sure what I'd expect this 
     # value to be... basically, whatever the RSpec designers 
     # felt made sense. 
     mysterious_method_to_get_depth.should == 3 
     end 
    end 
    end 
end 

实际上,我问,因为我要输出一些有用的信息,但以这样的方式,测试输出仍然是最大读取(即与适当的缩进)。

+0

我不不认为它是直接的*可能的,但你可以检查这个'调用者'回溯并找出它。 – d11wtq

回答

3

在您的示例中,您可以使用example.metadata,这是一个提供大量信息的散列。

0

继@Myron马斯顿的建议,我实现了这样的事情:

def mysterious_method_to_get_depth(meta) 
    if !meta.has_key?(:example_group) 
     0 
    else 
     1 + mysterious_method_to_get_depth(meta[:example_group]) 
    end 
end 

你应该称呼它:mysterious_method_to_get_depth(example.metadata)

另一种解决方案是定制DocumentationFormatter:https://stackoverflow.com/a/23446897/659788

相关问题