How to use Mock and Stub in Minitest and Rspec?
Referenced in Minimalicious testing in Ruby 1.9 with MiniTest
- A stub object is a pretend object that implement some of the interface of the object it pretends to be and returns predefined responses.
- A mock object is similair to a stub but has another use case: it helps decide if the test case it is used in passes by verifying if it’s methods has been called or not.
Minitest::Mock
require 'minitest/mock'
stub
1 2 3 4 5 6 |
|
mock
1 2 3 4 5 6 7 8 9 10 |
|
RSpec::Mocks
require 'rspec/mocks/standalone'
stub
1 2 3 4 5 6 7 8 9 10 |
|
mock
1 2 3 4 |
|