Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use mocking in production code?

Tags:

mocking

moq

I have a situation where I need to mock some code in production. This is for making one part of code, work in half functionality.

I have to choose to write an empty classes (to implement the interface) , or use a mocking system like moq.

So the question is, do the mocking systems hit the performance, or break some readability of the production code?

update
example:

interface IRocketSystem
{
   void LaunchTheRocket();
}

class SimulationRocketSystem:IRocketSystem
{
...
}

class RocketSystem:IRocketSystem
{
...
}

i found that i have a SimulationRocketSystem classes in production, that small and don't have a lot in the body. the mock system is have one line of code ( new Mock < IRocketSystem >().Object ) to replace classes like this.

the pros for mocking:
less empty classes in project.

like image 662
Avram Avatar asked Jul 31 '09 09:07

Avram


2 Answers

Sounds like a Null Object. I don't believe in using mocking frameworks for this for two reasons.

First, it hurts readability. You implementing the interface with an appropriately named empty class, then you document the intent inside that class. On the other hand, if you use a mocking framework, your documentation needs to be embedded in the code somewhere. Furthermore, it will be confusing as people tend to expect mocks in test code and not understand your intent for using mocks.

Second, you have to consider what happens if someone modifies the interface. What if a method is added? Should it default to returning null - as some frameworks would do? What if the method must return some specific return value according to the interface contract. If you have a concrete implementation, then the compiler will at least protect you somewhat. If you use a mocking framework, you may be out of luck.

like image 143
waxwing Avatar answered Sep 25 '22 01:09

waxwing


A mock object is something you use for testing, as it will let you assert that it was called correctly. It sounds to me that what you're looking for is more like a stub or a proxy object.

Since you will eventually implement the class in question it would make little sense to have a mock framework do it for you IMO. Why not just create the class and implement it as needed.

like image 25
Brian Rasmussen Avatar answered Sep 23 '22 01:09

Brian Rasmussen