rubydebuggingrspecrubymine

How can I run an rspec expect statement in a ruby debugger?


Whenver I want to write a semi-complex expectation, I start having to play around with matchers, seeing how can I combine them. For example, if I want to test that all hashes of an array of hashes don't have a key, my first instinct might be to write something like:

expect(the_array).to all(not(have_key(:the_key)))

Now, this does not work for my code, but it's not important. What I want to ask, is how can I add a breakpoint, and quickly iterate many times modifying the expectation? If I just run the expect line on the debugger the test ends, which means I get out of the debugging session.

Is there any "dry run" mode for expect so I can just get a true/false if the expect work, without actually exiting the test? Or can I run the second half of the expect statement on it's own?

I tried running the expect line in rubymine's debugger.

I'm expecting the line to execute without exiting debug mode.


Solution

  • There is an undocumented (internal) helper method of RSpec::Support.with_failure_notifier. You could use that to not raise an error on failure.

    RSpec::Support.with_failure_notifier(
      ->(failure, options) {
        puts failure, options
      }
    ) do
      expect(:foo).to(eq(:bar))
    end
    

    Or override the failure notifier permanently within your session

    RSpec::Support.failure_notifier = ->(failure, options) {
      puts failure, options
    }
    

    Now all expectations will just print out their failures rather than throw an error.