Search code examples
rubyseleniumselenium-webdriverrspecmatcher

Is there a way to use a matcher like 'Verify' in rspec?


So far I'm using 'expect' in my test framework which will stop the execution when it meets a fail condition. I want something like, the execution should happen even if it meets the fail condition. I could see that there is a matcher called 'Verify' in rspec where I need to inherit 'Test::Unit::TestCase' class, but my issue is that, I need the matcher in my spec file which is not written under a ruby class.


Solution

  • There isn't a way to do this with RSpec, out of the box. Because Rspec is designed to test small, isolated logics.

    On failure, Rspec matchers raise Error, So what you can do is to wrap the matchers in a rescue block. To satisfy your need, you could write a wrapper like this:

    def report_last(&block)
      begin
        yield
      rescue Exception => e
        puts "Failure: #{e}"
      end
    end
    

    In your test case:

    describe Calculator do
      it “should add 2 numbers” do
        report_last do
          expect(described_class.new(2, 3).sum)to eq(5)
        end
      end
    end