throwExceptionWithMessage

@trusted nothrow
throwExceptionWithMessage

Examples

It fails when an exception is not catched

Exception exception;

try {
  expect({}).to.throwException!Exception.withMessage.equal("test");
} catch(Exception e) {
  exception = e;
}

assert(exception !is null);
expect(exception.message).to.contain(`should throw exception with message equal "test". No exception was thrown.`);

It does not fail when an exception is not expected and none is not catched

Exception exception;

try {
  expect({}).not.to.throwException!Exception.withMessage.equal("test");
} catch(Exception e) {
  exception = e;
}

assert(exception is null);

It fails when the caught exception has a different type

Exception exception;

try {
  expect({
    throw new CustomException("hello");
  }).to.throwException!Exception.withMessage.equal("test");
} catch(Exception e) {
  exception = e;
}

assert(exception !is null);
expect(exception.message).to.contain("should throw exception with message equal \"test\". `fluentasserts.core.operations.throwable.CustomException` saying `hello` was thrown.");

It does not fail when a certain exception type is not catched

Exception exception;

try {
  expect({
    throw new CustomException("hello");
  }).not.to.throwException!Exception.withMessage.equal("test");
} catch(Exception e) {
  exception = e;
}

assert(exception is null);

It fails when the caught exception has a different message

Exception exception;

try {
  expect({
    throw new CustomException("hello");
  }).to.throwException!CustomException.withMessage.equal("test");
} catch(Exception e) {
  exception = e;
}

assert(exception !is null);
expect(exception.message).to.contain("should throw exception with message equal \"test\". `fluentasserts.core.operations.throwable.CustomException` saying `hello` was thrown.");

It does not fails when the caught exception is expected to have a different message

Exception exception;

try {
  expect({
    throw new CustomException("hello");
  }).not.to.throwException!CustomException.withMessage.equal("test");
} catch(Exception e) {
  exception = e;
}

assert(exception is null);

Meta