Skip to content
Cyrille DUPUYDAUBY edited this page Feb 6, 2018 · 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.

Exceptions

DoesNotThrow()

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

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

Throws<Type>()

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

// This check succeeds
Check.ThatCode( () =>
{
  throw new ApplicationException("GeneralFailure");
}).Throws<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");

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);