【发布时间】:2011-11-11 16:39:09
【问题描述】:
鉴于以下(基本)服务外观类,我想要一些关于哪些单元测试应该和值得编写的建议和指导。我正在使用 MEF 进行依赖注入,并使用 AutoMapper 将我的数据合约映射到域对象,反之亦然。
public sealed class MyServiceFacade
{
[ImportingConstructor()]
public MyServiceFacade(IDependency dependency,
IMappingEngine mappingEngine)
{
if (dependency == null)
throw new ArgumentNullException("dependency");
if (mappingEngine == null)
throw new ArgumentNullException("mappingEngine");
Dependency = dependency;
MappingEngine = mappingEngine;
}
public IDependency Dependency { get; private set; }
public IMappingEngine MappingEngine { get; private set; }
public ResponseContract TheMethod(RequestContract requestContract)
{
// Verify parameters
if (requestContract == null)
throw new ArgumentNullException(requestContract);
// Translate parameter values
var request = MappingEngine.DynamicMap<Request>(requestContract);
// Delegate to the domain layer
var response = Dependency.DoSomethingWith(request);
// Translate the response
var responseContract = MappingEngine.DynamicMap<ResponseContract>(response);
// Return the response
return responseContract;
}
}
我希望看到良好的代码覆盖率,但不想编写无用/无效的测试。
(有关有效集成测试的建议也会有所帮助。)
你的想法?
更新
基于有限的回应,我想我会继续通过描述我认为的“最坏情况”情景来进一步引导对话(我最初试图避免的事情)。
我团队中的一位开发人员大力提倡使用尽可能多的代码覆盖率进行“白盒”单元测试。由于他的方法,我们将进行以下测试:
- 构造对象时,如果'dependency'属性为null,则抛出ArgumentNullException。
- 构造对象时,如果“mappingEngine”属性为null,则抛出ArgumentNullException。
- 构造对象时,如果两个参数都不为null,则不会抛出ArgumentNullException。
- 对象构造完成后,Dependency属性将'dependency'参数中传入的对象返回给构造函数。
- 对象构造完成后,MappingEngine属性将'mappingEngine'参数中传入的对象返回给构造函数。
- 调用 TheMethod 时,如果 'requestContract' 属性为 null,则会引发 ArgumentNullException。
- 调用 TheMethod 时,如果“requestContract”属性不为 null,则不会引发 ArgumentNullException。
- 当调用 TheMethod 时,模拟的 IMappingEngine 的 DynamicMap() 方法只调用一次,并将 RequestContract 传递给 TheMethod。
- 调用 TheMethod 时,仅调用一次模拟 IDependency 的 DoSomethingWith() 方法,并使用从模拟 IMappingEngine 返回的 Request 对象。
- 调用 TheMethod 时,仅调用一次模拟 IMappingEngine 的 DynamicMap() 方法,并从模拟的 IDependency 返回 Response 对象。
- 调用 TheMethod 时,返回从模拟的 IMappingEngine 返回的 ResponseContract 对象。
如您所见,这会导致大量测试实际上是在测试实现,但并未反映更高级别的需求(另一种方法)。
这是您进行测试的方式还是会走不同的路线?
【问题讨论】:
-
你的问题比较模糊。你能说得更具体一点吗?
标签: unit-testing testing integration-testing