Question

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?

Was it helpful?

Solution

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 using statement of the assembly-under-test with .Moles appended 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 like MClass.BuildCustomerUpdatePlanListList = (List x, List y) => { [code]};. That defines an anonymous method that takes two Lists as parameters and you'd put whatever code wanted in the function. Just make sure that you return an IEnumerable in that anonymous method.

Here's an example using Moles to detour Directory.GetFiles:

using System.IO.Moles;
[assembly: MoledType(typeof(System.IO.Directory))]
...
    MDirectory.GetFilesStringString = (string x, string y) => new string[0];

Since the Directory class is a member of System.IO I use using 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.GetFiles takes 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 returns new 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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top