I’ve got a signature for a method that looks like this:
private IEnumerable BuildCustomerUpdatePlan(List localCacheChangedCustomers, List crmChangedCustomers){}
When I look at the moled object, the syntax (IntelliSense) of how to call the method and test itis absolutely confusing to me and every time I give it a shot, I get compilation errors. I’ve looked through the basic tutorials provided on MSFT’s site, but I simply don’t get how to test a private method using Moles or how to deal with the return type and multiple parameters.
Unfortuantely I’ve been unable to find other good HOWTO’s or threads demonstrating a more complex sample than just working with a simple Add() method that spits out an INT and accepts an INT. 🙁
Tips?
In your testing project, first make sure you add a Moles assembly corresponding to the assembly-under-test. You’ll also want to add an
usingstatement of the assembly-under-test with.Molesappended so you can use the moled assembly.Moles changes the names of the classes and methods to the form
M[Original Class Name].[Original Method Name][typeof param1][typeof param2].... In your case a detour for that method could look likeMClass.BuildCustomerUpdatePlanListList = (List x, List y) => { [code]};. That defines an anonymous method that takes twoLists as parameters and you’d put whatever code wanted in the function. Just make sure that you return anIEnumerablein that anonymous method.Here’s an example using Moles to detour
Directory.GetFiles:Since the
Directoryclass is a member ofSystem.IOI useusing System.IO.Moles;to specify that I want to use moled members of the assembly.Moles requires you to specify the types Moled:
[assembly: MoledType(typeof(System.IO.Directory))]does the job.Finally,
Directory.GetFilestakes two strings as parameters and returns a string array. To detour the method into returning the equivalent of no files found, the moled method just returnsnew string[0]. Curly braces are needed if you want multiple lines in the anonymous method and, if not detouring a void method, a return statement that matches the type the original method would return.