Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is Mocking?

What is Mocking?                                                                                                    .

like image 388
masoud ramezani Avatar asked Apr 19 '10 07:04

masoud ramezani


People also ask

What does mocking a person mean?

1 : to treat with contempt or ridicule : deride he has been mocked as a mama's boy— C. P. Pierce. 2 : to disappoint the hopes of for any government to mock men's hopes with mere words and promises and gestures— D. D. Eisenhower.

Is mocking a good thing?

Mocking is a very common testing mechanism, and it is a bad idea. This post details why you should not use mocking, and why and how you should write integration tests instead. TL;DR: Mocking provides false confidence by hiding real failures.

What is an example of a mock?

The definition of mock is something fake, or something arranged for practice. A knock-off of a designer purse is an example of a mock purse. A trial that is practice for the real trial is an example of a mock trial. Mockery, the act of mocking.

What is the purpose of mocking?

The purpose of mocking is to isolate and focus on the code being tested and not on the behavior or state of external dependencies. In mocking, the dependencies are replaced by closely controlled replacements objects that simulate the behavior of the real ones.


1 Answers

Prologue: If you look up the noun mock in the dictionary you will find that one of the definitions of the word is something made as an imitation.

Mocking is primarily used in unit testing. An object under test may have dependencies on other (complex) objects. To isolate the behavior of the object you want to replace the other objects by mocks that simulate the behavior of the real objects. This is useful if the real objects are impractical to incorporate into the unit test.

In short, mocking is creating objects that simulate the behavior of real objects.

At times you may want to distinguish between mocking as opposed to stubbing. There may be some disagreement about this subject but my definition of a stub is a "minimal" simulated object. The stub implements just enough behavior to allow the object under test to execute the test.

A mock is like a stub but the test will also verify that the object under test calls the mock as expected. Part of the test is verifying that the mock was used correctly.

To give an example: You can stub a database by implementing a simple in-memory structure for storing records. The object under test can then read and write records to the database stub to allow it to execute the test. This could test some behavior of the object not related to the database and the database stub would be included just to let the test run.

If you instead want to verify that the object under test writes some specific data to the database you will have to mock the database. Your test would then incorporate assertions about what was written to the database mock.

like image 179
Martin Liversage Avatar answered Oct 10 '22 15:10

Martin Liversage