Yesterday I blogged about fakewin8, a set of components that leverage code generation to create fake classes, which can be used to simplify unit testing in environments where dynamic proxy generation is not a viable option. If you are developing Windows Store or Windows Phone apps you should take it for a spin to see how it feels like.
Today’s blog post explains how fakewin8 allows you to define valid parameters for fake methods.
A bit of context
Commonly, when creating unit tests you need to setup constraints for mock method invocations. This is usually done by providing predicates or specific values for the parameters with which a method must be invoked. If these are not met, your test should fail.
In fakewin8 you can configure fake methods to only accept invocations that match a certain set of predicates based on its parameters. If an invocation does not match a specified predicate an
InvalidInvocationException is thrown. To specify constraints for a parameter of type
T, a predicate of type
Func<T, bool> must be used. For example:
[gist id=04e749d3d85e0f6b2d71 file=Accept.cs]
For the common scenario where any possible value is acceptable for a particular parameter, you can use Any<T>.IsOK() which creates a Func<T, bool> that always returns true:
[gist id=04e749d3d85e0f6b2d71 file=Any.cs bump=1]