Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing software: fake vs stub

There are quite a few written about stub vs mocks, but I can't see the real difference between fake and stub. Can anyone put some light on it?

like image 550
Vadim Samokhin Avatar asked Jul 24 '11 16:07

Vadim Samokhin


People also ask

What is the difference between stub and fake?

There are different test doubles with different purposes—fakes, mocks, and stubs. Fakes are objects that have working implementations. On the other hand, mocks are objects that have predefined behavior. Lastly, stubs are objects that return predefined values.

What are fakes in testing?

Fake. Fakes are objects that have working implementations, but not same as production one. Usually they take some shortcut and have simplified version of production code.

What is a test stub in software testing?

A stub is a small piece of code that takes the place of another component during testing. The benefit of using a stub is that it returns consistent results, making the test easier to write. And you can run tests even if the other components are not working yet.

What is a stub example?

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.


1 Answers

I assume you are referring to the terminology as introduced by Meszaros. Martin Fowler does also mentions them regularly. I think he explains the difference pretty well in that article.

Nevertheless, I'll try again in my own words :)

A Fake is closer to a real-world implementation than a stub. Stubs contain basically hard-coded responses to an expected request; they are commonly used in unit tests, but they are incapable of handling input other than what was pre-programmed.

Fakes have a more real implementation, like some kind of state that may be kept for example. They can be useful for system tests as well as for unit testing purposes, but they aren't intended for production use because of some limitation or quality requirement.

like image 89
Thorarin Avatar answered Oct 05 '22 17:10

Thorarin