簡體   English   中英

有沒有辦法在RSpec中獲得當前的范圍級別?

[英]Is there a way to get the current scope level in 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

我實際上是在問,因為我想輸出一些有用的信息,但是這樣的方式使得測試輸出仍然是最大可讀的(即,使用適當的縮進)。

在您的示例中,您可以使用example.metadata ,它是一個提供大量信息的哈希。

根據@Myron Marston的建議,我實現了這樣的事情:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM