Skip to content

Latest commit

 

History

History
99 lines (72 loc) · 2.16 KB

callable.md

File metadata and controls

99 lines (72 loc) · 2.16 KB

Callable API

up

Here are the examples of how you can use the should function with exceptions.

Summary

Examples

Throw exception

You can check if some code throws or not Exceptions. The throwException template will return a ThrowableProxy which for convience it has all the Throwable properties. A simple way of checking an exception message is:

    ({
        throw new CustomException("test");
    }).should.throwException!CustomException.msg.should.equal("test");

The ThrowableProxy it also have a withMessage method that returns a new should structure, initialized with the exception message:

    ({
        throw new CustomException("test");
    }).should.throwException!CustomException.withMessage.equal("test");

Success expectations

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

    ({ }).should.not.throwException!CustomException;

Failing expectations

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

    ({ }).should.throwException!CustomException;

Throw any exception

The throwAnyException assert is an alias for throwException!Exception

Success expectations

    ({
        throw new Exception("test");
    }).should.throwAnyException;

    ({ }).should.not.throwAnyException;

Failing expectations

    ({
        throw new Exception("test");
    }).should.not.throwAnyException;

    ({ }).should.throwAnyException;

Throw something

The throwSomething assert is an alias for throwException!Throwable

  ({
    assert(false, "test");
  }).should.throwSomething.withMessage.equal("test");

Execution time

Success expectations

    ({ }).should.haveExecutionTime.lessThan(1.msecs);

Failing expectations

    ({
      Thread.sleep(2.msecs);
    }).should.haveExecutionTime.lessThan(1.msecs);