Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is wrong with Stubs for unit testing?

I just watched this funny YouTube Video about unit testing (it's Hitler with fake subtitles chewing out his team for not doing good unit tests--skip it if you're humor impaired) where stubs get roundly criticized. But I don't understand what wrong with stubs.

I haven't started using a mocking framework and I haven't started feeling the pain from not using one.

Am I in for a world a hurt sometime down the line, having chosen handwritten stubs and fakes instead of mocks (like Rhinomock etc)? (using Fowler's taxonomy)

What are the considerations for picking between a mock and handwritten stub?

like image 894
MatthewMartin Avatar asked Oct 05 '09 23:10

MatthewMartin


People also ask

What is a unit testing stub?

A stub is an object that holds predefined data and uses it to answer calls during tests. It is used when you can't or don't want to involve objects that would answer with real data or have undesirable side effects. An example can be an object that needs to grab some data from the database to respond to a method call.

Should I use mock or stub?

Mocks should be used when you want to test the order in which functions are called. Stubs verify the state of the system under test. Stubs don't take order into account, which can be helpful for reducing the work of rewriting tests when code is refactored.

What errors are commonly found during unit testing?

Unit testing considerations What errors are commonly found during Unit Testing? (1) Misunderstood or incorrect arithmetic precedence, (2) Mixed mode operations, (3) Incorrect initialization, (4) Precision inaccuracy, (5) Incorrect symbolic representation of an expression.


1 Answers

There is nothing wrong with stubs, there is room for stubs, mocks... and spies. All are "test doubles", but with different purposes as explained in Mocks and Stubs aren't Spies:

[...] Before moving on, I'd like to clarify and define some terms in use here, which I originally discovered in Gerard Meszaros' xUnit Patterns book.

  • A Dummy Object is a placeholder object passed to the system under test but never used.
  • A Test Stub provides the system under test with indirect input
  • A Test Spy provides a way to verify that the system under test performed the correct indirect output
  • A Mock Object provides the system under test with both indirect input and a way to verify indirect output

[...] And you can let this handy chart guide your decisions:

alt text

PS: Mockito - The New Mock Framework on the Block is worth the read too.

like image 130
Pascal Thivent Avatar answered Sep 25 '22 22:09

Pascal Thivent