Is there a way to get the current volume level in RSpec?

This is absolutely stupid and unimportant, but I'm just curious: using RSpec , can I somehow get access to what is β€œdepth” What am I in now? It...

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

I really ask because I want to print some useful information, but so that the test output is still as readable as possible (i.e. with the right indent).

+3
source share
2 answers

In your examples, you can use example.metadatawhich is a hash that provides a ton of information.

+3
source

Following @Myron Marston's suggestion, I implemented something like this:

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

You should call it like this: mysterious_method_to_get_depth(example.metadata)

DocumentationFormatter: fooobar.com/questions/1907365/...

0

All Articles