本文介绍了在MSpec中处理匿名方法时,是否存在用于设置[HostType("Moles")]的解决方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Pex和Moles进​​行低级单元测试,但是我也在探索MSpec进行业务逻辑验证,并且希望继续使用Moles来保持一致性.我认为问题在于MSPec使用匿名方法,因此无法应用HostType("Moles")属性.例如:

I'm using Pex and Moles for my low-level unit testing, but I'm also exploring MSpec for business-logic validation, and would like to keep using Moles for consistency. The problem, I think, is that MSPec uses anonymous methods, so there's no way to apply the HostType("Moles") attribute. For example:

Because of = () =>
   employeeList = EmployeeManager.GetUsersByRoles(rolesToLoad);

It should_return_a_list_of_employees = () =>
   employeeList.ShouldNotBeNull();

我正在嘲笑在"GetUsersByRoles"内部调用的Roles提供程序,当我尝试通过MSpec运行此测试时,我收到标准的"Moles要求测试在被检测的进程中"错误,并附有添加[ HostType("Moles")]转到我的测试方法.这里有任何解决方法或其他选项吗?

I'm mocking the Roles provider called inside "GetUsersByRoles," and when I try to run this test via MSpec, I get the standard "Moles requires tests to be IN an instrumented process" error, with the instruction to add [HostType("Moles")] to my test method. Is there any workaround or other option available here?

旁注:我已经下载了MSMSpec.tt并对其进行了修改,以在生成的VSTests中包括该属性,但是我希望能够直接通过其自己的运行程序或TestDriven.net运行MSpec测试,以便获得广管局和企业主的友好成果.

Side note: I have downloaded MSMSpec.tt and modified it to include the attribute on the generated VSTests, but I'd like to be able to run the MSpec tests directly via its own runner or TestDriven.net so I can get the friendly output for BAs and business owners.

推荐答案

解决方法是将匿名方法替换为非匿名方法.基本上不可能使用Mspec.

The workaround is to replace the anonymous method with one that is not. Moling Mspec is basically not possible.

Moles无法绕道匿名方法.之所以这样,是因为这些方法必须是可寻址的,否则将被绕道而行.匿名方法不是隐式可寻址的,因为它们是在运行时生成和引用的.简而言之,您不能通过该类调用匿名方法,因为它是... anonymous .

Moles is not capable of detouring anonymous methods. The reason why is that the methods must be addressable, to be detoured. Anonymous methods are not implicitly addressable, because they are generated and referenced during runtime. Simply put, you can not call an anonymous method through the class, because it is, well... anonymous.

Moles手册指出:"Moles可用于绕过任何.NET方法,包括密封类型的非虚拟方法和静态方法."因此,在Moles使用反射来标识类成员的假设下进行操作是一个安全的选择.不能通过委托,Action或Func调用的任何东西都不能被骚扰.

The Moles Manual states, "Moles can be used to detour any .NET method, including non-virtual and static methods in sealed types." Therefore, operating under the assumption that Moles uses reflection to identify class members is a safe bet. Anything that can not be called via delegate, Action, or Func, can not be moled.

这篇关于在MSpec中处理匿名方法时,是否存在用于设置[HostType("Moles")]的解决方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 06:05