xp-framework / unittest

Unittests for the XP Framework
0 stars 0 forks source link

Implement Assert::throws() #41

Closed thekid closed 4 years ago

thekid commented 4 years ago

This pull request implements a new method throws() in the assertion DSL. With it, code along the lines of the following can be rewritten to a more concise form:

Before

use lang\IllegalArgumentException;

class FixtureTest {

  #[@test]
  public function raises_exception() {
    $fixture= ... // Code which could also raise IllegalArgumentExceptions
    try {
      $fixture->test(null);
      $this->fail('No exception raised', null, IllegalArgumentException::class);
    } catch (IllegalArgumentException $expected) {
       // OK
    }
  }
}

After

use lang\IllegalArgumentException;

class FixtureTest {

  #[@test]
  public function raises_exception() {
    $fixture= ... // Code which could also raise IllegalArgumentExceptions
    Assert::throws(IllegalArgumentException::class, function() use($fixture) {
      $fixture->test(null);
    });
  }
}