throwAnyException

@trusted nothrow
throwAnyException

Examples

It should be successfull when the function does not throw

void test() {}
expect({ test(); }).to.not.throwAnyException();

It should fail when an exception is thrown and none is expected

void test() { throw new Exception("Test exception"); }

bool thrown;

try {
  expect({ test(); }).to.not.throwAnyException();
} catch(TestException e) {
  thrown = true;

  assert(e.message.indexOf("should not throw any exception. `object.Exception` saying `Test exception` was thrown.") != -1);
  assert(e.message.indexOf("\n Expected:No exception to be thrown\n") != -1);
  assert(e.message.indexOf("\n   Actual:`object.Exception` saying `Test exception`\n") != -1);
}

assert(thrown, "The exception was not thrown");

It should be successfull when the function throws an expected exception

void test() { throw new Exception("test"); }
expect({ test(); }).to.throwAnyException;

It should not be successfull when the function throws a throwable and an exception is expected

void test() { assert(false); }

bool thrown;

try {
  expect({ test(); }).to.throwAnyException;
} catch(TestException e) {
  thrown = true;

  assert(e.message.indexOf("should throw any exception. A `Throwable` saying `Assertion failure` was thrown.") != -1);
  assert(e.message.indexOf("\n Expected:Any exception to be thrown\n") != -1);
  assert(e.message.indexOf("\n   Actual:A `Throwable` with message `Assertion failure` was thrown\n") != -1);
  assert(e.file == "source/fluentasserts/core/operations/throwable.d");
}

assert(thrown, "The exception was not thrown");

It should be successfull when the function throws an expected exception

void test() { throw new Exception("test"); }
expect({ test(); }).to.throwAnyException;

Meta