Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the use of mocks a good programming practice or just a different way to do it? [closed]

Would you guys say that the use of mocks is better than not using mocks? Is mocking used only in unit testing or could it be used directly in the original project as the real object and switch it after?

I've been reading here and there and the most attractive thing about mocking I found was the layer isolation.

like image 828
Arturo Avatar asked Jul 24 '10 01:07

Arturo


People also ask

What is the purpose of mocks?

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.

Why or when is it useful to test using mocks?

Only use a mock (or test double) “when testing things that cross the dependency inversion boundaries of the system” (per Bob Martin). If I truly need a test double, I go to the highest level in the class hierarchy diagram above that will get the job done. In other words, don't use a mock if a spy will do.

What is the benefit of mocking?

In general case, mocking allow one to: Test behaviour (when something happens, then a particular method is executed) Fake resources (for example, email servers, web servers, HTTP API request/response, database)

What does it mean to mock programming?

Mocking means creating a fake version of an external or internal service that can stand in for the real one, helping your tests run more quickly and more reliably. When your implementation interacts with an object's properties, rather than its function or behavior, a mock can be used.


2 Answers

mocks are definitely useful in unit testing. When you want to test A that relies on B, in isolation, then you mock B (with expected input/output) to test A. You make sure you test B separately to make sure it is correct.

In dynamic languages, they aren't strictly necessary. But mocking frameworks can help you verify that the expectations you set on the mock are met.

You should never use a mock as a real implementation. That type of thing is the stuff of jokes. "We can just mock the entire application! Yaaaaay!" Thats what interfaces/equivalent are for...

like image 128
hvgotcodes Avatar answered Oct 26 '22 23:10

hvgotcodes


Ultimately, it can be argued that it is "just another way to do it." Software was written for decades and continues to be written in the absence of mocking.

It is primarily a facility of unit testing.

Where mocking/stubs really shine is test-driven development. The problem one faces in the absence of mocking/stubbing is that the web of dependencies in an application is often such that you have to build almost the entire application before you can write tests, and even then you're testing larger sections of functionality, such that isolating bugs becomes more difficult.

Consider the following:

  • I want to create ClassA and write tests to verify its behaviour.
  • I start writing the test (test-first to drive out the design).
  • Oops, ClassA needs to get data from RepositoryM.
  • I'll implement RepositoryM so that I can test ClassA.
  • Oh, but first let's write tests for RepositoryM.
  • Darn, RepositoryM really needs ServiceX to populate its data.
  • I'll implement ServiceX so I can test and implement RepositoryM so I can test and implement ClassA

…and on and on.

Using mocks allows you to start writing tests without implementing anything. All you need are interfaces.

Using a mocking framework makes test construction much faster.

Mocking/stubbing has ancillary benefits -- one of which is enforcement of programming to abstractions instead of implementations.

like image 38
Jay Avatar answered Oct 27 '22 00:10

Jay