Skip to content
Ludovic Alarcon edited this page May 11, 2020 · 10 revisions

Check.That(Async)Code is the entry points you will use to check bits of code or delegates. Use Check.ThatAsyncCode for asyc methods/delegates, Check.ThatCode otherwise. All following checks can be used with CheckThatCode as well as CheckThatAsyncCode.

Exceptions

DoesNotThrow()

Checks that the code does not throw any exception; fails if any exception is thrown.

// This check succeeds
Check.ThatCode( () =>
{}).DoesNotThrow();
// This check fails
Check.ThatCode( () =>
{
  throw new Exception();
}).DoesNotThrow();

Throws<Type>(), ThrowsType(_Type) (V2.2+)

Checks that the code does throw an exception of type Type; fails if no exception or any other exception is thrown.

// Those checks succeed
Check.ThatCode( () =>
{
  throw new ApplicationException("GeneralFailure");
}).Throws<ApplicationException>();
Check.ThatCode( () =>
{
  throw new ApplicationException("GeneralFailure");
}).ThrowsType(typeof(ApplicationException>));
// Those checks fail
Check.ThatCode( () =>
{
}).Throws<ApplicationException>();
Check.ThatCode( () =>
{
  throw new Exception();
}).Throws<ApplicationException>();

ThrowsAny()

Checks that the code does throw an exception; fails if no exception is thrown.

// This check succeeds
Check.ThatCode( () =>
{
  throw new ApplicationException("GeneralFailure");
}).ThrowsAny();
// This check fails
Check.ThatCode( () =>
{
}).ThrowsAny();

Extensions to Exception checks

DueTo<Type>()

Checks that the raised exception has an inner exception of the expected Type; fails otherwise.

// This check succeeds
Check.ThatCode( () =>
{
  throw new Exception("GeneralFailure", new ApplicationException("rootCause"));
}).Throws<Exception>().DueTo<ApplicationException>("GeneralFailure");
// This check fails
Check.ThatCode( () =>
{
  throw new Exception("GeneralFailure", new SystemException("rootCause"));
}).Throws<Exception>().DueTo<ApplicationException>("GeneralFailure");

WithMessage(expected)

Checks that the thrown exception is raised with the expected message; fails otherwise.

// This check succeeds
Check.ThatCode( () =>
{
  throw new Exception("GeneralFailure");
}).Throws<ApplicationException>().WithMessage("GeneralFailure");
// This check fails
Check.ThatCode( () =>
{
  throw new ApplicationException("Failure");
}).Throws<ApplicationException>().WithMessage("GeneralFailure");

AndWhichMessage()

Not a check. When you need to perform more complex check on the error message that simple equality to an expected value, AndWhichMessage allows you to perform any check on the error message as a string. Example:

// This check succeeds
Check.ThatCode( () =>
{
  throw new Exception("GeneralFailure");
}).Throws<ApplicationException>().AndWhichMessage().StartsWith("General");
// This check succeeds
Check.ThatCode( () =>
{
  throw new ApplicationException("Failure");
}).Throws<ApplicationException>().AndWhichMessage().EndsWith("Failure");

WhichMember(propertyExpression) (V2.7)

Not a check. Allows to perform checks on any property(ies) of a raised exception. Example:

// This check succeeds
Check.ThatCode( () =>
{
  throw new ArgumentNullException("theParam");
}).Throws<ArgumentNullException>().WhichMember( x => x.ParamName).IsEqualTo("theParam");

Results extension

WhichResult<Type>()

Not a check. Allows to perform checks on the result of the function checked thanks to Check.ThatCode or Check.ThatAsyncCode

// This check succeeds
Check.ThatCode( () => 42).DoesNotThrow().And.WhichResult().IsEqualTo(2);

Performance

LastsLessThan(duration, unit)

Checks that the code is executed within a given duration, expressed in unit of time; fails otherwise.

// This check succeeds
Check.ThatCode( () =>
{
  for (var i = 0; i < 100; i++) {}
}).LastsLessThan(10, TimeUnit.Millisecons);
// This check fails
Check.ThatCode( () =>
{
  for (var i = 0; i < 5; i++)
  {
    Thread.Sleep(10);
  }
}).LastsLessThan(10, TimeUnit.Millisecons);

ConsumesLessThan(duration, unit)

Checks that the code CPU time consumption is below a given duration, expressed in unit of time; fails otherwise. Note that on Windows system, CPU time measure has a resolution of 10-15 ms, therefore you should use this check on code consumming 50ms of CPU time or more.

// This check succeeds
Check.ThatCode(c() =>
{
  Thread.Sleep(20);
}).ConsumesLessThan(100, TimeUnit.Milliseconds);

// This check fails (code explicitely consumes 40ms of computation time)
Check.ThatCode(
() =>
{
  var timer = new Stopwatch();
  timer.Start();
  while (timer.ElapsedMilliseconds < 40)
  {
    for (var i = 0; i < 1000000; i++)
    { var unused = i * 2;}
  }
}).ConsumesLessThan(10, TimeUnit.Milliseconds);
Clone this wiki locally