单个方法有多个测试. [英] More than one test for a single method.

查看:120
本文介绍了单个方法有多个测试.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种检查多种业务逻辑的方法.我需要为每种业务逻辑编写一种测试方法.有可能吗?

I have a method which checks for multiple business logics. I need to write a test method for each business logics. Is it possible?

基本上,该方法在每次检查两次业务逻辑之间执行一系列动作.例如,如果我在方法中具有n个动作和n个业务逻辑.要为第n个逻辑创建测试方法,我需要通过所有先前的逻辑.

basically the method does a sequece of actions in between each achtion we check some business logic. For example if i have n actions and n business logics in the method. To create the test method for n'th logic i need to pass through all previous logics.

请帮助我.

 

推荐答案

您好,Muthusam,

Hi Muthusam,

通常,您应该组合方法中的逻辑.保持苗条和卑鄙;即使用依赖注入来分离逻辑.我假设您要使用Entlib框架中的Unity(使用4.1 或5.0)

In general you should not combine logic in methods. Keep them lean and mean; i.e. use Dependency injection to separate the logic. I'm assuming you are going to use Unity from the Entlib framework (use 4.1 or 5.0)

这对您的情况意味着:

  1. 您需要将逻辑拆分为一个或多个单独的帮助程序类
  2. 在它们各自的接口上创建接口
  3. 重构现有代码,以便对帮助程序类进行调用(您现在最终得到了一个控制器类/方法,该类/方法调用了其他类上的方法)
  4. 在类的构造函数中或使用[Dependency]属性实例化帮助器类(请在Entlib文档中查找)
  5. 在测试方法中使用存根来存根您不想测试的任何呼叫.
  1. you need to split up your logic into one or more separate helper classes
  2. create interfaces on each of them
  3. refactor you existing code so that it makes calls to the helper classes (you now end up with a controller class / method that invokes methods on other classes)
  4. Instantiate the helper classes in either the constructor of the class or use [Dependency] attributes (look this up in the Entlib documentation)
  5. Use stubs in your test method to stub out any of the calls you do not want to test.

对于存根,我建议使用Rhino Mocks或Moq.看看他们的文档是如何执行的.总而言之,这并非易事,但您将从学习经验/更好的设计中受益匪浅.它应该,但是大约 从0点开始的第一天(假设您不熟悉这些技术中的任何一种).
For stubs I would recommend Rhino Mocks or Moq. Have a look at their documentation how this is performed. All in all, this is not trivial to do, but you will benefit greatly from the learning expierence / better design. It should, however take about a day to master starting from point 0 (assuming you are not familiar with any of these techniques).


这篇关于单个方法有多个测试.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆