Quantcast
Channel: What's the difference between a mock & stub? - Stack Overflow
Viewing all articles
Browse latest Browse all 43

Answer by Paul Sweatte for What's the difference between a mock & stub?

$
0
0

A stub is an empty function which is used to avoid unhandled exceptions during tests:

function foo(){}

A mock is an artificial function which is used to avoid OS, environment or hardware dependencies during tests:

function foo(bar){ window = this; return window.toString(bar); }

In terms of assertions and state:

  • Mocks are asserted before an event or state change
  • Stubs are not asserted, they provide state before an event to avoid executing code from unrelated units
  • Spies are setup like stubs, then asserted after an event or state change
  • Fakes are not asserted, they run after an event with hardcoded dependencies to avoid state

References


Viewing all articles
Browse latest Browse all 43

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>