Rspec contexts running together when separate due to name

26 Views Asked by At

I have some tests like this

context 'Main block' do
    let(:variable) { 'test' }
    pseudo and tests

end

context 'Main block two' do
    let(:variable2) { 'test2' }
    pseudo and tests

end

context 'Main block three' do
    let(:variable3) { 'test3' }
    pseudo and tests

end

Using RubyMine, when I run the tests for the first context using the ui, it actually runs all three contexts. If I switch one of the last two main blocks context name e.g. xxx Main block two instead of Main block two then it will exclude that context. Similarly, if I change the name of Main block to Main block one then it will only run that context as intended, excluding the 2nd and 3rd contexts.

It would seem that RubyMine will run subsequent contexts if their name begins with an exact match of the entire intended context.

I've looked for a while and am unable to find a reasoning but I'm thinking this may be a setting or perhaps even bad practice, but I'm looking for the reason it's happening. Would anyone have insight into this?

1

There are 1 best solutions below

0
engineersmnky On

From the man page you can see option -e states: "Run examples whose full nested names include STRING". From Source

If you look at the run options and you will see that it uses a regex for matching e.g. full_description: /Main block/ which will obviously match all of the above.

There is another option -E which allows you to specify your own Regex to use however it will still use the full nested name so you would have to go with something like -E "\AMain block (?!(one|two))\" which if you have a lot of these could be very awkward.

A better way separate tests is usually to use tagging e.g.

context 'Main block', :one do
    let(:variable) { 'test' }
    pseudo and tests

end

context 'Main block two', :two do
    let(:variable2) { 'test2' }
    pseudo and tests

end

context 'Main block three', :three do
    let(:variable3) { 'test3' }
    pseudo and tests

end

You can add this to an RSpec configuration in RubyMine by supplying

  • Runner options: --tag one (For running tests tagged as :one)

Your other option is avoid the duplication of matched patterns for the named contexts.