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

jfelchner picture jfelchner  ·  3Comments

benmmurphy picture benmmurphy  ·  6Comments

ankit8898 picture ankit8898  ·  3Comments

kevinlitchfield picture kevinlitchfield  ·  6Comments