Skip to content

Instantly share code, notes, and snippets.

@mfilej
Last active November 5, 2021 09:16
Show Gist options
  • Save mfilej/5466144 to your computer and use it in GitHub Desktop.
Save mfilej/5466144 to your computer and use it in GitHub Desktop.
RSpec hook to show current spec in process name

Our suite would sometimes randomly hang when running a certain spec, but we weren't able to tell which one. With the below RSpec around filter you can see the spec that is currently running by inspecting the process name.

$ rspec spec_spec.rb &
[2] 64968
$ ps | grep rspec
64968 ttys005    0:00.24 rspec /Users/miha/spec_spec.rb:10 "Dummy spec waits for 100 seconds"
RSpec.configure do |c|
c.around :each do |example|
title = example.metadata[:full_description]
source = example.metadata[:example_group_block].source_location.join ":"
$0 = %{rspec #{source} "#{title}"}
example.run
end
end
describe "Dummy spec" do
it "waits for 100 seconds" do
sleep 100
end
end
@mislav
Copy link

mislav commented Apr 26, 2013

Cool hack. I think minitest listens to SIGINFO (ctrl-T, if I remember) which you can hit to get information about the current test.

@topherhunt
Copy link

+1 to this. I needed to autogenerate unique and meaningful VCR cassette names for the current example. In Minitest, getting the example string is as easy as self.class.to_s + @__name__, but it looks like Rspec makes you do extra work.

We solved this using a global before hook to save the example string to an instance variable which is available throughout the example:

# in spec_helper.rb
RSpec.configure do |config|
  # ... other config stuff
  config.before(:each) do |example|
    # ... other config stuff
    @spec_description = example.metadata[:full_description]
  end
end

Then we name our VCR cassette after that string:

# in some helper method
name = @spec_description.gsub(/[^\w]/, '').downcase
VCR.use_cassette(name) do
  # ...

@jipiboily
Copy link

Exactly what I needed! Thanks so much @mfilej for sharing!!! ❤️

@jgonzalezd
Copy link

Thanks a lot! It worked perfectly.
Just one thing metadata[:example_group_block] is deprecated. Use metadata[:block] instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment