如何为“moled”分配/选择多个代表方法?
目前,我在等待 VS 2010 许可证的同时,正在从外部检查 Moles,我想知道 Moles 是否允许我:
- 提供为正在调用的方法分配多个 Moles 委托的能力,也许是在测试装置设置级别?
- 在我的测试用例中运行时切换,对于即将对被隔离的 moled 方法进行的调用,必须调用我的哪一个 moled 委托?
有什么提示吗?
I am currently examining Moles from the outside while I wait for my VS 2010 license, and I wonder whether Moles allows me to:
- provide the ability to assígn multiple mole delegates for a method being moled, perhaps at a test fixture setup level?
- switch in runtime in my test case, which of my mole delegates must be invoked for the upcoming call(s) to the moled method being isolated?
Any hints?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最佳答案:
在绕行方法中包含门逻辑比对同一方法使用两个存根要容易得多,也更有意义!例如,MyMethod 从磁盘上的三个不同文件读取数据,每个文件都需要返回不同的模拟数据。我们可以绕道System.IO.File.OpenRead,通过分析OpenRead的输入参数来控制返回值:
测试方法:
目标类型:
直接回答您的问题:
对 #1 是:为每个绕道实例化一种类型,然后将每种类型用于所需的行为。并且,对#2 是:对摩尔类型的一个实例或另一种实例采取行动。这需要添加方法输入参数或类构造函数注入。
例如,MyMethod从磁盘读取三个数据文件,您需要传回三个不同的数据模拟。 MyMethod 需要三个参数,这是一个明显侵入性的解决方案。 (注意输入参数是FileInfo类型;因为System.IO>File是静态的,无法实例化:例如:
TEST METHOD:
TARGET TYPE:
UPDATE:
In response对于@Chai 评论,可以在测试项目中创建通用方法,这些方法可以被引用为mole detour delegate,例如,您可能希望编写一个可以被任何单元测试引用的通用方法,该方法设置。各种 示例展示了如何使用参数化方法——它们只是方法调用
!
下面的
ArrangeScenarios() 通过打开枚举参数来设置摩尔绕道。这允许在许多测试中以 DRY 的方式建立相同的场景。
Best Answer:
It is much easier and makes far more sense to include gating logic in the detour method, than using two stubs for the same method! For example, MyMethod reads data from three different files on disk, each requiring different mock data to be returned. We may detour System.IO.File.OpenRead and gate the return value by analyzing the input parameters of OpenRead:
TEST METHOD:
TARGET TYPE:
Direct Answer to Your Questions:
Yes to #1: instantiate one type for each detour, and then use each for the desired behavior. And, yes to #2: act upon one instance of the mole type or the other. This requires addition of method input parameters or class constructor injection.
For example, MyMethod reads three data files from disk, and you need to pass back three different data mocks. MyMethod requires three parameters, an overtly intrusive solution. (Note input parameters are FileInfo type; because, System.IO>File is static and can not be instantiated: For example:
TEST METHOD:
TARGET TYPE:
UPDATE:
In response to @Chai comment, it is possible to create common methods, within the test project, that may be referenced as the mole detour delegate. For example, you may wish to write a common method that may be referenced by any unit test, that sets up a variety of pre-configured scenarios. The following example displays how a parameterized method could be used. Get creative -- they're just method calls!
TARGET TYPES:
TEST METHODS:
ArrangeScenarios() sets up mole detours, by switching on the enumeration parameter. This allows the same scenarios to be erected, in a DRY manner, throughout many tests.