問題描述
我是 mockito 的新手.
I am new to mockito.
需要知道存根和何時(shí)的區(qū)別
need to know difference between stub and when
1. stub(cpproxy.getBinList()).toReturn(gettestbins());
2. when(cpproxy.getBinList()).thenReturn(gettestbins());
這兩者有什么區(qū)別?
推薦答案
其實(shí)它們?cè)诩夹g(shù)上是一樣的.最初創(chuàng)建 Mockito 時(shí),我們談?wù)摰氖谴娓虼嗽~匯表遵循了這個(gè)想法.后來人們認(rèn)為用交互而不是技術(shù)術(shù)語來思考更好,所以詞匯遵循when ... then ...風(fēng)格.詞匯的這種變化有助于人們思考對(duì)象之間的交互、消息傳遞.這是面向?qū)ο笳Z言中最有趣的想法(消息傳遞)(引用 Alan Kay).
Actually they are technically the same. When Mockito was first created, we were talking about stubs, so the vocabulary followed that idea. Later people thought it was better to think in interactions rather that technical terms, so the vocabulary followed the when ... then ... style. This change in vocabulary helps people to think about interactions, messaging between object. Which is the most interesting idea (message passing) thing in an object oriented language (quoting Alan Kay).
如今的測(cè)試方法已演變?yōu)樾袨轵?qū)動(dòng)開發(fā)(來自 Dan North),這幾乎是相同的東西,但更多地關(guān)注設(shè)計(jì)時(shí)的行為.為了反映這種想法,人們要求 Mockito 提供反映這種變化的 API.因此,您還可以使用 BDDMockito
Nowadays testing approach has evolved to Behavior Driven Development (from Dan North), which is almost the same thing but focus even more on the behavior at design time. To reflect that thinking, people asked Mockito to offer an API that reflect that change. So you also use given ... will ... style from BDDMockito
given(the_type.performs_that()).willReturn(something)
這是我現(xiàn)在最喜歡的詞匯,因?yàn)槲沂褂脺y(cè)試來驅(qū)動(dòng)我的對(duì)象設(shè)計(jì).
This is my preferred vocabulary now as I use tests to drive my objects design.
這篇關(guān)于存根和模擬時(shí)的區(qū)別的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!