测试替身

在测试的时候,我们难免会使用测试替身帮助我们聚焦测试的关注点。测试替身主要有5种。

Dummy

Dummy objects are passed around but never actually used. Usually they are just used to fill parameter lists.

dummy这个东西会被传入测试方法中,但是不会被测试方法使用,基本都是作为测试方法的占位符,现在测试中基本不会出现。

Fake

Fake objects actually have working implementations, but usually take some shortcut which makes them not suitable for production (an in memory database is a good example).

Fake是假冒伪劣产品,基本功能还是有的,但是都是缩水版的,不能用在生产环境。比如,数据可能要从数据库取,而Fake的就是从本地文件中取。也不经常用到

Stubs

Stubs provide canned answers to calls made during the test, usually not responding at all to anything outside what's programmed in for the test.

Stubs俗称打桩。它是一组定义好的request/response集合。当request找不到的时候,response则为空。

Spies

Spies are stubs that also record some information based on how they were called. One form of this might be an email service that records how many messages it was sent.

Spies指的是间谍,它不会改变原有代码的行为,但是会在运行测试的时候记录一些你需要的信息。

Mock

Mocks are what we are talking about here: objects pre-programmed with expectations which form a specification of the calls they are expected to receive.

Mock定义了不同的请求的expection是什么,只关注方法/行为,不关注结果和实现的细节

你可能感兴趣的:(测试替身)