Rspec-core: How to get spec's name inside `let` block

Created on 28 May 2020  ยท  5Comments  ยท  Source: rspec/rspec-core

Hi thanks for the awesome gem as always :)

I want to get spec's name(= context name?) inside let block but IDK if this is possible or not ๐Ÿค” would help if you can answer ๐Ÿ™Œ thanks.

RSpec.describe Foo do
  describe 'bar' do
    context 'when something is happening' do
      let(:spec_name) { # would be awesome if I can get "Foo bar when something is happening returns true" here }

      it 'returns true' do
      end
    end
  end
end

All 5 comments

Hi @take
For sure it's possible:

      let(:spec_name) do |example|
        example.metadata[:full_description] # => "Foo bar when something is happening returns true"

      end

I believe you can use the parameter for it as well to get such information in the example itself.
There are much more other things that are accessible there.

You're always welcome!

aha thank you so much! ๐Ÿ™Œ

@pirj

do you know which version of rspec-core includes this feature? :eyes:

in my rspec-core 3.9.2 app, I get the following error ๐Ÿค”

Failure/Error: example.metadata[:full_description]

NoMethodError:
  undefined method `metadata' for nil:NilClass

@take Can you please provide a little more context what your code looks like?
The key is the do |example| in let(:spec_name) do |example|.

sorry problem is solved, thanks for the help! ๐Ÿ™Œ

Was this page helpful?
0 / 5 - 0 ratings

Related issues

wwood picture wwood  ยท  5Comments

ankit8898 picture ankit8898  ยท  3Comments

fabioperrella picture fabioperrella  ยท  3Comments

fimmtiu picture fimmtiu  ยท  3Comments

alexcoplan picture alexcoplan  ยท  3Comments