帮助使用 Moles 语法测试带有泛型的私有方法
我有一个方法的签名,如下所示: private IEnumerable BuildCustomerUpdatePlan(List localCacheChangedCustomers, List crmChangedCustomers){}
当我查看moled对象时,如何调用该方法和测试的语法(IntelliSense)对我来说绝对令人困惑,每次我尝试一下,我都会得到编译错误。我已经浏览了 MSFT 网站上提供的基本教程,但我根本不知道如何使用 Moles 测试私有方法或如何处理返回类型和多个参数。
不幸的是,我无法找到其他好的 HOWTO 或线程来演示比仅使用简单的 Add() 方法(吐出 INT 并接受 INT)更复杂的示例。 :(
尖端?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的测试项目中,首先确保添加与被测程序集相对应的 Moles 程序集。您还需要添加被测程序集的
using
语句,并附加.Moles
,以便您可以使用模型化的程序集。Moles 将类和方法的名称更改为
M[原始类名].[原始方法名][typeof param1][typeof param2]...
的形式。在您的情况下,该方法的绕行可能类似于 MClass.BuildCustomerUpdatePlanListList = (List x, List y) => {[代码]};。它定义了一个匿名方法,该方法采用两个List
作为参数,并且您可以在函数中放入所需的任何代码。只需确保在该匿名方法中返回一个IEnumerable
即可。下面是一个使用 Moles 绕行
Directory.GetFiles
的示例:由于
Directory
类是System.IO
的成员,因此我使用using System .IO.Moles;
指定我要使用程序集的 moled 成员。Moles 要求您指定 Moled 类型:
[assemble: MoledType(typeof(System.IO.Directory))]
完成这项工作。最后,
Directory.GetFiles
将两个字符串作为参数并返回一个字符串数组。为了使该方法返回相当于未找到文件的结果,moled 方法仅返回new string[0]
。如果您希望在匿名方法中使用多行,并且如果不绕过 void 方法,则需要使用与原始方法将返回的类型相匹配的返回语句。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 likeMClass.BuildCustomerUpdatePlanListList = (List x, List y) => { [code]};
. That defines an anonymous method that takes twoList
s as parameters and you'd put whatever code wanted in the function. Just make sure that you return anIEnumerable
in that anonymous method.Here's an example using Moles to detour
Directory.GetFiles
:Since the
Directory
class is a member ofSystem.IO
I 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.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 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.