SwensenSoftware / unquote

Write F# unit test assertions as quoted expressions, get step-by-step failure messages for free
http://www.swensensoftware.com/unquote
Apache License 2.0
285 stars 26 forks source link

New assertion checker: `trap` #135

Closed eiriktsarpalis closed 6 years ago

eiriktsarpalis commented 6 years ago

I've been using the following custom assertion checker in my test projects for quite some time:

val trap : Expr<'T> -> 'T

This evaluates the expression tree as normal, returning the result if successful but outputing the full evaluation metadata if exceptional. This can be quite useful for asserting preconditions during a test run, e.g.

let! response1 = server.Get()
let interestingPart = trap <@ response1.Value.items.[42] @>
let! response2 = server.Post interestingPart
test <@ expected = response2 @>

Notice how trap gives me the confidence of performing unsafe lookups like .Value and .[42] since any failure will render as a nice assertion exception.

stephen-swensen commented 6 years ago

@eiriktsarpalis nice! This fits perfectly along side the other assertion operators. I'll merge it and try to get a release out ASAP.

Thanks!