帮助使用 Moles 语法测试带有泛型的私有方法

发布于 2024-12-03 08:29:34 字数 357 浏览 2 评论 0原文

我有一个方法的签名,如下所示: 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

稍尽春風 2024-12-10 08:29:34

在您的测试项目中,首先确保添加与被测程序集相对应的 Moles 程序集。您还需要添加被测程序集的 using 语句,并附加 .Moles ,以便您可以使用模型化的程序集。

Moles 将类和方法的名称更改为 M[原始类名].[原始方法名][typeof param1][typeof param2]... 的形式。在您的情况下,该方法的绕行可能类似于 MClass.BuildCustomerUpdatePlanListList = (List x, List y) => {[代码]};。它定义了一个匿名方法,该方法采用两个 List 作为参数,并且您可以在函数中放入所需的任何代码。只需确保在该匿名方法中返回一个 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 类型:[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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文