throwException

@trusted nothrow
throwException

Examples

Should be able to catch a certain exception type

expect({
  throw new CustomException("test");
}).to.throwException!CustomException;

It should fail when an unexpected exception is thrown

bool thrown;

try {
  expect({
    throw new Exception("test");
  }).to.throwException!CustomException;
} catch(TestException e) {
  thrown = true;

  assert(e.message.indexOf("should throw exception \"fluentasserts.core.operations.throwable.CustomException\".`object.Exception` saying `test` was thrown.") != -1);
  assert(e.message.indexOf("\n Expected:fluentasserts.core.operations.throwable.CustomException\n") != -1);
  assert(e.message.indexOf("\n   Actual:`object.Exception` saying `test`\n") != -1);
  assert(e.file == "source/fluentasserts/core/operations/throwable.d");
}

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

It should not fail when an exception is thrown and it is not expected

expect({
  throw new Exception("test");
}).to.not.throwException!CustomException;

It should fail when an different exception than the one checked is thrown

bool thrown;

try {
  expect({
    throw new CustomException("test");
  }).to.not.throwException!CustomException;
} catch(TestException e) {
  thrown = true;
  assert(e.message.indexOf("should not throw exception \"fluentasserts.core.operations.throwable.CustomException\".`fluentasserts.core.operations.throwable.CustomException` saying `test` was thrown.") != -1);
  assert(e.message.indexOf("\n Expected:no `fluentasserts.core.operations.throwable.CustomException` to be thrown\n") != -1);
  assert(e.message.indexOf("\n   Actual:`fluentasserts.core.operations.throwable.CustomException` saying `test`\n") != -1);
  assert(e.file == "source/fluentasserts/core/operations/throwable.d");
}

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

Meta