在您的测试项目中,首先确保添加与被测组件相对应的 Moles 组件。您还需要添加using
被测程序集的语句,并.Moles
附加,以便您可以使用摩尔程序集。
Moles 将类和方法的名称更改为 form M[Original Class Name].[Original Method Name][typeof param1][typeof param2]...
。在您的情况下,该方法的绕道可能看起来像MClass.BuildCustomerUpdatePlanListList = (List x, List y) => { [code]};
. 这定义了一个匿名方法,它接受两个List
s 作为参数,你可以在函数中放入任何想要的代码。只需确保您IEnumerable
在该匿名方法中返回一个。
这是一个使用 Moles 绕道的示例Directory.GetFiles
:
using System.IO.Moles;
[assembly: MoledType(typeof(System.IO.Directory))]
...
MDirectory.GetFilesStringString = (string x, string y) => new string[0];
由于Directory
该类是System.IO
我using System.IO.Moles;
用来指定我想使用moled 程序集的成员的成员。
Moles 要求您指定类型 Moled:[assembly: MoledType(typeof(System.IO.Directory))]
完成这项工作。
最后,Directory.GetFiles
将两个字符串作为参数,并返回一个字符串数组。为了绕过该方法返回等效于未找到文件,moled 方法仅返回new string[0]
. 如果您希望匿名方法中有多行,并且如果不绕行 void 方法,则需要大括号,则需要与原始方法将返回的类型匹配的 return 语句。