NewLearner
NewLearner

Reputation: 41

Does Mock call Real Objects?

Kindly let me know about these.

Upvotes: 0

Views: 1546

Answers (1)

Andreas Gnyp
Andreas Gnyp

Reputation: 1850

What is a unit-test

A unit-test suggests you only want to see your functionality in a small environment, typically a class. But this class often talks to other classes and those talk to other classes... which can mean a lot of work, just to get this one class going. Also, you just want to test your unit. This is, where mocks/stubs come in. I will explain the distinction later.

What you do, is - instead of recreating your whole app - to make assumptions. A mock is basically an assumption. Everything your class needs, is being mocked or stubbed. This can also be the case for already tested parts of your unit. You assume, then you test one specific (real) part of your unit. Then mock this one, if necessary, and so on.

Now what is the difference between mocks and stubs.

Both are assumptions, but mocks are also expectations. So, if you use a mock, your testing-framework should complain, if it hasn't been used/called. A stub on the other hand, is just for the case. Like a random call to the Internet, lookups to database, which are not important for the test per se.

What your framework actually creates, depends on the framework and the language. But it will/should not call the original method, as explained above. Usually you'd have to tell this explicitly to the framework, which sometimes might make sense.

Upvotes: 1

Related Questions