-
Mocking a generic method (that uses TResult) with Microsoft Moles
Situation There is a generic method that uses TResult which you want to mock using Microsoft Moles. Problem You can’t just assign a delegate like on a non-generic method. Solution Create a delegate that behaves like the mocked method should. Define the concrete expected parameter types. Instantiate a new MolesDelegates.Func with the corresponding type-parameters and…
-
“QTAgent32.exe reagiert nicht mehr” appears when running unit-tests
Situation You are writing unit-tests and maybe are refactoring the implementation along the way. Problem “All of a sudden” QTAgent32.exe crashes when you run the tests. Solution A unit-test (or changed implementation) most probably caused a stack overflow. Attach the debugger to QTAgent32.exe (use the “Debug” button on the crash-window), so you can see the…
-
Using Microsoft Moles and dynamics (DLR)
Situation You want to use the ‘dynamic’ type in your unit-test. Problem An InvalidOperationException that says ‘Dynamic operations can only be performed in homogenous AppDomain.’ is thrown. Solution Changed the attribute ‘enabled’ of the node ‘legacyCasPolicy’ to ‘false’ in the following configuration file: C:Program Files (x86)Microsoft Visual Studio 10.0Common7IDEPrivateAssembliesMicrosoft.Moles.VsHost.x86.exe.config Sources http://weblogs.asp.net/mjarguello/archive/2011/04/26/using-moles-with-dlr.aspx http://www.cameronfletcher.com/post/InvalidOperationException-Dynamic-operations-can-only-be-performed-in-homogenous-AppDomain.aspx http://social.msdn.microsoft.com/Forums/en/pex/thread/0a931fc1-9049-482a-bf2d-5499ccf65b82 http://stackoverflow.com/questions/4230909/odd-exception-in-mvc-3-project
-
MessageBox in a unit-test
Situation You want to unit-test a method which uses a MessageBox. Problem The MessageBox will pop up and stop the execution of all tests until the user chooses an action (e.g. presses “OK”). Solution One way around this issue is mocking the MessageBox class to just return the expected DialogResult, which can be done using…
-
Using reflection to unit-test private methods
Situation There is a class called “MyClass” which has a private method called “MyPrivateMethod” (which expects a single string parameter) that you want to unit-test. Problem You can’t access private methods from the test class by just calling them. Solution Use reflection to invoke the method. typeof(MyClass).GetMethod(“MyPrivateMethod”, BindingFlags.NonPublic | BindingFlags.Instance) .Invoke(null, new object[] { “MyStringParameter” }); Hint: Static methods require the BindingFlag…