Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fake objects vs Mock objects

In the TDD there is two concept: fake objects and mock objects. These two concepts are used in case a class you want to test is interacting with other classes or objects or databases...

My question is : what is the difference between the two? and when can I use each one of them?

Edit: I found this answer: What's the difference between faking, mocking, and stubbing?

But I'm still confused about the difference between the two: both of them create implementation of the components, with light implementation for a Fake. But, what do they mean by "light implementation" of "shortcut" in case of a fake? And what is the difference between how a Mock object works, and the real object work?

like image 349
younes sofiane Avatar asked Dec 15 '15 11:12

younes sofiane


People also ask

What are mock objects?

In object-oriented programming, a mock object is a simulated object that mimics the behavior of the smallest testable parts of an application in controlled ways.

What is the difference between stub and mock objects?

Stub: Stub is an object that holds predefined data and uses it to answer calls during tests. Such as: an object that needs to grab some data from the database to respond to a method call. Mocks: Mocks are objects that register calls they receive.

Why should you use a mocks fakes in writing test cases?

You use mocks when you don't want to invoke production code or when there is no easy way to verify, that intended code was executed. There is no return value and no easy way to check system state change. An example can be a functionality that calls e-mail sending service.

What is the difference between mock and spy?

Mocks are used to create fully mock or dummy objects. It is mainly used in large test suites. Spies are used for creating partial or half mock objects. Like mock, spies are also used in large test suites.


1 Answers

A fake implementation for a DataSet for instance, would simply return a static set of data. A mock would pretty much be a full implementation that would be able to generate various sets of data depending upon input. If you were mocking away your data layer, you would be able to execute your command objects against the mock and it would be robust enough to return data with a "valid" statement, or throw exceptions with an invalid statement. All without actually connecting to a database or file.

As for the differences between mock and real, usually when mocking a class away, you would create a factory object that, by default, returns the real object, but when you write your tests, you can tell it to return a specific mock class. The mock class either implements the same interfaces as the real object, or you use a wrapper class that mimics the underlying real class, but allows for dependency injection for the critical parts to generate data without making the external calls.

like image 95
gmiley Avatar answered Oct 02 '22 15:10

gmiley