Rhino Mocks Arrange Act Assert and expectation on generic methods
I have a project where some actions are to be scheduled by an external library. Each action can be instantiated by a constructor or by a static factory method, and I want to verify with the test that: all action declares static factory method and inside the factory methods all objects are resolved by a call to IoC.Resolve<T>. Since all the actions are in a specific assembly into a specific namespace, I wrote this test.
|
|
This test is simple, it load the assembly with actions, then iterate trough all types, and if the type belong to a specific namespace, it verify the presence of a static public method called CreateCommand, then call that method and verify that the return object is of correct type.
This test is flawed, because if you mistype the name of the namespace as I did (typed Myproject instead of MyProject) the test will succeed because no type is inspected. This is the typical situation where the test have complex code (reflection) and the test passed, not because it verifies the correct expectations, but because the test itself is bugged :). The solution is simple.
|
|
In this way we are sure that at least one type is analyzed.
But the test is still not so good, it not only tests the presence of the Factory Method, but it depends from the IoC engine, since all factories methods relay on method IoC.Resolve<ActionType>(). So I need a way toverify that each factory method call the right IoC method and return the object created by the IoC engine. A possible solution is this test
|
|
But it does not work, because in factory method my commands does not call IoC.Resolve(typeof(xxxx)) but IoC.Resolve<xxxx>(), where xxxx is the type of the command, here is the typical implementation.
|
|
Now I can change all factory methods to call other method, but I really do not like to do this. The test should be write in this way.
|
|
But since the type is known only a run-time, it is not possible to write this expectation natively or at least I did not know it (what do you write instead of ??? in the snippet above? ). You can be tempted to use c.Resolve<Object>, but this does not work, since in this way rhino mocks expects a real call to Resolve<Object> and will not match a call such as Resolve<SchedulableWebReview>(). A possible solution is the following one
|
|
The solution was to recreate with ExpressionTree the real lambda that you cannot create at compile time, it is quite simple, and is composed by these steps
- Create the ParameterExpression of type IWindsorContainer
- Get the Resolve<T>() methodInfo with reflection
- Use the MakeGenericMethod to create a MethodInfo for Resolve<Type> where type is passed at runtime
- Create a MethodCallExpression that represents a call to this method.
- Create the lambdaExpression
Now I create an instance of the Command type with Activator.CreateInstance, and finally thanks to the Compile() method of lambda expression I’m able to set the expectation :). Finally I invoke the factory method, and set an assertion that verifies that return value of the factory method is the object set as return in the expectation.
I do not know if this method is really good but it works. It has the drawback of using the Compile() method of the LambdaExpression, that is really slow at runtime, and can slow down your unit test suite. In my computer the whole test (with 18 commands to test, thus 18 calls to Compile()) runs in 1.50 secs, but most of the time is spent initializing the test. If I run the whole test fixture class (10 tests) it runs in 1.60 secs, and if I remove this test and run the 9 remaining test execution time is 1.50 approx, and this is fine.
alk.
Tags: Unit Test Rhino Mock