单个方法有多个测试. [英] More than one test for a single method.
问题描述
我有一种检查多种业务逻辑的方法.我需要为每种业务逻辑编写一种测试方法.有可能吗?
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)
这对您的情况意味着:
- 您需要将逻辑拆分为一个或多个单独的帮助程序类
- 在它们各自的接口上创建接口
- 重构现有代码,以便对帮助程序类进行调用(您现在最终得到了一个控制器类/方法,该类/方法调用了其他类上的方法)
- 在类的构造函数中或使用[Dependency]属性实例化帮助器类(请在Entlib文档中查找)
- 在测试方法中使用存根来存根您不想测试的任何呼叫.
- you need to split up your logic into one or more separate helper classes
- create interfaces on each of them
- 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)
- Instantiate the helper classes in either the constructor of the class or use [Dependency] attributes (look this up in the Entlib documentation)
- Use stubs in your test method to stub out any of the calls you do not want to test.
这篇关于单个方法有多个测试.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!