What are different types of test doubles and their uses?

What are different types of test doubles and their uses?

问题

I was going through an online course on test driven development and came across the concept of test doubles. As per the definition of test double in the course :

Test Doubles : Test doubles are objects that are used in unit tests as replacement to the real production system collaborators.

I got an idea what test doubles mean. But then it was mentioned there are various types of test doubles. The ones mentioned in the course were :

Dummy : Objects that can be passed around as necessary but do not have any type of test implementation and should never be used.

Fake : These objects generally have a simplified functional implementation of a particular interface that is adequate for testing but not for production.

Stub : These objects provide implementation with canned answers that are suitable for the tests.

Spies : These objects provide implementation that record the values that were passed in so they can be used by the tests.

Mocks : These objects are pre-programmed to expect specific calls and parameters and can throw exceptions when necessary.

I have worked with mocks before and do have a brief idea of what they are and how to use them. Though I was confused regarding the other mentioned types of test doubles.

Can someone help me with difference between these types of test doubles and when to use one ?

回答1

Useful literature,

Dummy is sort of an odd case, in that you are using a dummy because the code under test doesn't actually use the dummy; in other words, the composition of the system is requiring you to supply some superfluous elements. Often, this is a hint that the logic you are testing should be accessible via a more specific interface.

Stubs and Fakes are typically used when a test scenario requires that the code under test obtains values from some dependency.

A stub is very focused; it doesn't pretend to do the right thing internally, but instead just returns some canned answer. For instance, you might use a stub to mimic a specific failure mode.

Fake is badly named - it's a real implementation of the dependency, just one that is optimized for testing (small, deterministic, in memory) rather than optimized for production (scale). In some circles, you'll hear substitute, rather than "fake".

Spies and mocks are typically used when you are trying to determine that your code sends the right information to a dependency, without the costs of being coupled to that dependency.

Spies normally maintain a history of messages that they receive during testing, which can later be verified against an expected message list. Mocks are a more active expression of that idea, where assertions are made about the behavior of the code under test while the scenario is running, rather than afterwards.

 

作者:Chuck Lu    GitHub    
posted @   ChuckLu  阅读(17)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2022-03-15 what port or ports are used for File sharing in windows?
2019-03-15 FAQ Flyway
2019-03-15 Baseline
2019-03-15 First Steps: Command-line
2019-03-15 Concepts-->Migrations
2019-03-15 How Flyway works
2019-03-15 Why database migrations?
点击右上角即可分享
微信分享提示