Mock Objects 和 Stubbed Objects 是什么意思


在IT测试中这两个词的意思:
stub:  a short projecting piece or part.     测试对象中的一部分
mock: to copy someone or a characteristic of someone.   模仿测试对象的特征  。

https://stackoverflow.com/questions/3459287/whats-the-difference-between-a-mock-stub

Mocks 是行为测试
Stubs  是状态测试

一次只测一件事,一个测试里面可以有多个stubs,但只有一个mock

stubs 的测试过程是:准备测试对象和合作者;执行;检查对象状态;清理资源。
mocks 的测试过程是:准备测试对象;准备对象达到的预期。执行;检查方法是否正确执行;检查对象的状态;清理资源。

总结:
mocks和stubs测试都会给一个答案:“结果是什么?”
mocks测试更加关注的是:“这个结果是怎么得到的?”


http://jstest.jcoglan.com/mocking.html
These terms are often confused so I'll clarify what I mean by them.

Stubbing means replacing a method, function or an entire object with a version that produces hard-coded responses. This is typically used to isolate components from each other, and your code from the outside world. For example, stubbing is often used to decouple tests from storage systems and to hard-code the result of HTTP requests to test code that relies on data from the internet.

stubbing是用...一段硬编码的回应...替换一个方法,函数或者一整个对象。分离各组件(解耦), 常用 decouple分离测试和存储系统,例如:硬编码返回https请求,用来测试依赖互联网传回的数据

Mocking is a form of testing that involves verifying behaviour by checking which methods are called during a test. Like stubbing, it involves replacing methods with fake versions, but it also means setting expectations that those methods must be called. This is used to specify contracts between layers of an application, and to test side-effects.

Mocking 是测试的形式用来验证行为,检查哪些方法被调用。 像stubbing, 他也用fake versions替换方法, 但是他也意味着要 设置方法被调用返回的expectations结果。 这被用来指定应用各层直接的contract契约, 也要测不良作用。

 https://github.com/marlenabowen/mocks_vs_stubs

mocks和stubs的不同点在于如何设计对象, 
Mock 对象设计用来 exhibit behavior(调用方法)。 
Stubbed objects 设计来exhibit state(  exhibit 展示)。

Example:
我们有个鞋店,通过app下订单, app有个Order对象(订单对象)一个Store对象, order 对store对象有个依赖,通过一个方法:place_order (下订单)
class Order
  def place_order(store, number_of_shoes)
    store.purchase(number_of_shoes)
  end
end

我们对place_order的测试需要Store这个对象,并调用他的对象的 purchase方法。
为了排除不确定性,我们想创建一个test double for Store
To rule out non-determinism in our test, we want to create a test double for Store

a mock of Store would look like this:

class MockStore
  def purchase(number_of_shoes)
      "mock_store.purchase called for #{number_of_shoes} shoes"
  end
end

这个mock告诉我们中特purchase方法被调用 with some number_of_shoes,  然后那就是我们的unit test 将要assert 断言的。

A stub of Store Would look like this:

class StoreStub
  def init(number_of_shoes_in_stock)
    @number_of_shoes_in_stock = number_of_shoes_in_stock
  end

  def purchase(number_of_shoes)
    @number_of_shoes_in_stock  -= number_of_shoes
  end
end

这个Stub 保持track of the state of the StoreStub (how many shoes are in stock). 我们的unit test 单元测试将assert that 这个正确的鞋子数量is left after the method call。

require_relative "test_doubles/MockStore.rb"
require_relative "test_doubles/StoreStub.rb"
require_relative "../order.rb"

describe Order do
 context "Using a mock" do
  describe "place_order" do
   mock_store = MockStore.new()
   order = Order.new()
   it "places an order at a store for a number of shoes" do
    expect(order.place_order(mock_store, 5)).to eq("MockStore.purchase called for 5 shoes")
   end
  end
 end

 context "Using a stub" do
  describe "place_order" do
   store_stub = StoreStub.new(20)
   order = Order.new()
   it "places an order at a store for a number of shoes" do
    order.place_order(store_stub, 5)
    expect(store_stub.shoes_in_stock).to eq(15)
   end
  end
 end
end

总结:
mock方法,是声明了Mock对象模拟方法体内的对象,用来测试 方法的主体(主程序),判断方法是否能运行并返回结果。
stub的测试方法,是声明了stubbed对象模拟方法体内的对象,测试 method里面的函数,可能存在多个函数调用。

mock和stub就是2个概念,如果更通俗一点可能可以这样说,mock测得粗糙点,看表面,stub测得细一些,看内在。
阅读量: 468
发布于:
修改于: