Difference between stub, mock and fake in Unit Test ChatGPT

In unit testing, "stubs", "mocks", and "fakes" are all dummy假的 implementations of functions or objects that are used to test the behavior of another piece of code. However, they differ in the degree to which they are configured and the role they play in the testing process.

 

A "stub" is a very simple dummy假的 implementation of a function or object that is used to return a predetermined预先确定的 value or simulate a specific condition. Stubs are often used to provide input values for the code under test or to test error handling.

 

A "mock" is a more sophisticated dummy implementation that can be configured to verify that certain conditions are met during the test.

Mocks can be set up to verify that certain functions are called, certain values are returned, or certain conditions are met.

 

A "fake" is a working implementation of a function or object that is used in place of the real implementation for testing purposes.

Fakes are often used when the real implementation is too complex or time-consuming to set up for a test, or when the real implementation has external dependencies that are difficult to manage in a test environment.

 

In general, stubs are the most basic form of dummy implementation and are used to provide input values or simulate simple conditions. Mocks are more advanced and are used to verify the behavior of the code under test. Fakes are fully functional implementations that are used to substitute for the real implementation in tests.

作者:Chuck Lu    GitHub    
posted @   ChuckLu  阅读(32)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2021-01-09 Javascript中prototype属性详解
2021-01-09 Javascript 构造函数
2020-01-09 Updates to the Git Commit Graph Feature
2020-01-09 Choose a process in Azure DevOps
2020-01-09 Agile Vs Scrum: Know the Difference
2019-01-09 KestrelHttpServer
2019-01-09 notepad++ 使用技巧
点击右上角即可分享
微信分享提示