-3

I want to understand what needs to be mocked and what not when writing test cases in general.

For example, we will mock I/O operations, but what about functions imported from another module. Are we supposed to mock them as well?

Erik Cederstrand
  • 9,643
  • 8
  • 39
  • 63
Pravin
  • 677
  • 2
  • 7
  • 22
  • 1
    There are many unit testing tutorials a quick google away! – SuperShoot Mar 26 '19 at 06:59
  • They doesn't talk about insights what developer thinks while mocking something, that is what need to understand from experienced developer. – Pravin Mar 26 '19 at 07:01
  • 1
    My opinion is your question is too broad for this platform, however others may disagree and I hope you find the answer you are looking for. – SuperShoot Mar 26 '19 at 07:03
  • We are solving specific questions here. Giving turorials is not a service we can offer. – Klaus D. Mar 26 '19 at 07:03
  • To know what experienced developers think you have to be an experienced developer. Anyways, that is off topic on stackoverflow – zvone Mar 26 '19 at 07:54

2 Answers2

2

Mocking should be done for a reason. Good reasons are:

  • You can not easily make the depended-on-component (DOC) behave as intended for your tests.
  • Does calling the DOC cause any non-derministic behaviour (date/time, randomness, network connections)?
  • The test setup is overly complex and/or maintenance intensive (like, need for external files)
  • The original DOC brings portability problems for your test code.
  • Does using the original DOC cause unnacceptably long build / execution times?
  • Has the DOC stability (maturity) issues that make the tests unreliable, or, worse, is the DOC not even available yet?

For example, you (typically) don't mock standard library math functions like sin or cos, because they don't have any of the abovementioned problems.

Dirk Herrmann
  • 5,550
  • 1
  • 21
  • 47
0

You really have to know what you are unit testing. From there it will be clear what to mock...