vendredi 24 mars 2017

Is there an equivalent of Java's AssertionError in Ruby?

I'm trying to write some test helpers for a gem, and would like to provide some assertions that will trigger test failures where appropriate.

In Java, this is achievable (if I recall) by throwing an AssertionError, which should then get picked up by the test runner and Just Work. Does anything like this exist for Ruby?

For example, let's say I have a gem emily-post, which provides some helpful methods for checking how polite a request is:

request = 'Can I haz cheezburger?'
EmilyPost.is_polite?(request) # false

request = 'Can I haz cheezburger, please?'
EmilyPost.is_polite?(request) # true

Let's say as part of this gem I want to provide test helpers that can:

  • Set up an (im)polite request
  • Verify that the response is also polite

For example my test might read like:

include EmilyPost::TestHelpers

with_polite_request do
    response = 'No way!'
    issue(response) # This rude response should fail the test
end

The code for with_polite_request might look something like:

def with_polite_request
    request = 'Can I haz cheezburger, please?'
    yield
    raise AssertionError unless is_polite?(response)
end

...where AssertionError would preferably be some error recognised by any test framework as a failure, rather than an error.

Does any such error exist?

Aucun commentaire:

Enregistrer un commentaire