何时使用部分模拟? [英] When to use partial mocks?

查看:83
本文介绍了何时使用部分模拟?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始对假货,存根,模拟和动态模拟的想法感到满意.但是对于何时使用部分模拟,我还是有些疑惑.

I'm starting to get comfortable with the idea of fakes, stubs, mocks, and dynamic mocks. But I am still a little iffy in my understanding of when to use partial mocks.

如果您打算对服务进行模拟,并且需要诉诸部分模拟,那么这似乎是不良设计的标志.难道部分模拟主要是为了使遗留代码处于测试覆盖范围内?

It would seem that if you're planning on mocking a service and need to resort to a partial mock then it is a sign of bad design. Is it that partial mocks are mostly for getting legacy code under test coverage?

另一方面,我正在测试一个具有Reset()方法的类.如果我已经在单独的测试中确认Reset()方法可以工作,并且我具有应以对该方法的调用结尾的类的某些功能,那么对对象进行部分模拟并运行该测试设计是否不好?针对部分模拟进行测试,在Reset()方法上定义了Expectation.

On the flip side of this, say I am testing a class which has a Reset() method. If I have already confirmed in a separate test that the Reset() method works, and I have some functionality of the class that should end with a call to this method, is it poor test design to do a partial mock of the object and run tests against the partial mock, defining an Expectation on the Reset() method.

我目前已经以这种方式进行了几次测试,这种事情以后会让我惹上麻烦吗?

I currently have several tests set up in this manner, is this sort of thing going to get me in trouble later on?

推荐答案

在您的示例中,听起来Reset方法是实现细节,并且通过使用部分模拟,您有将测试耦合到实现的危险班上的.这会使您的测试变得比需要的更脆弱.

In your example it sounds like the Reset method is an implementation detail and by using a partial mock, you are in danger of coupling your test to the implementation of the class. This will make your test more brittle than it needs to be.

我还认为,使一个对象具有一些带有实际实现的方法和一些带有存根实现的方法,会使测试变得更加混乱.稍后再返回测试时,要记住的只是一件事.

I also think it makes tests more confusing to have an object having some methods with real implementations and some with stub implementations. It's just one more thing to remember when you come back to a test later.

您可以(a)使用基于状态的测试来断言在内部调用了真正的Reset方法之后对象的状态是否符合您的期望;或(b)使用基于交互的测试来验证是否使用真正的Reset方法对协作对象进行了相关调用?

Can you either (a) use state-based testing to assert that the state of the object is as you expect after the real Reset method has been called internally; or (b) use interaction-based testing to verify that the relevant calls to collaborating objects have been made as a result of the real Reset method?

您可能会找到测试气味:模拟具体类可用.

这篇关于何时使用部分模拟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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