-
Notifications
You must be signed in to change notification settings - Fork 53
Check.ThatCode
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.
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();
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>();
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();
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");
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");
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);
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);
- Welcome
- How to start
- Customising error messages
-
Check.That
- All (Equality, Type)
- Reference types
- Members based
- IEnumerable (Properties, Content, Elements)
- String (Properties, Content, RegExp )
- Numeric Type(Properties, Comparisons, Floating)
- Dictionary
- Char (Properties, Value)
- IComparable
- DateTime
- DateTimeOffset
- Misc: Boolean, TimeSpan, Stream, Enum, EventWaitHandle
- Check.ThatCode
- Check.ThatDynamic
- Extensibility
- Auxiliary types (TimeUnit)
- Mocks