Jump to content
  • Advertisement
Sign in to follow this  

What do you think about mocks?

This topic is 2387 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello unit testers,

What do you think about mocks?

Personally, I find it handy that mocking frameworks allow you to create fake/stub objects that you can control to return certain things and so on. The mocking frameworks use reflection for that, so pretty easy to do.

However, that is what fake or stub objects are. The REAL intention of mocks, is to set "expectations" about how many times exactly each method of your mock object is called, and with which parameters.

In some cases, using these expectations in tests can be handy, e.g. if you're testing some class that has the purpose of calling certain functions of some other object.

However, in most cases, a class is NOT like that. In most cases, a class (or function) is supposed to do certain things, and what I'm interested in testing is that it gives a correct result. How it achieves that result, how many times and in which order it called certain functions for that, I often don't care about that. Only about the result.

Yet there are people who use mocks for this anyway, with tests that break if you'd implement the class in a different way such that it achieves the same result but with a different algorithm that happens to call certain functions in a different order.

Do you know what I'm talking about? What's your opinion about this?
Do you use mocks? Do you like using mocks as much as possible?
Do you use mocks as "stubs" or "fakes", or do you use them for expectations of how many times a function is called?
Do you write your unit tests to test the result, or the implementation?
What are the advantages of using mocks with expectations, even for classes that are related to some algorithm where only the result is of interest?

Note, I may not be 100% correct about the little details of the terminology of "stubs", "fakes" and "mocks". If you wish to correct me, feel free. What I mean in this post is, by "mock" I mean something on which you set expectations of how often its functions are called, while by "stub"/"fake", I mean a simple implementation of something that is otherwise too hard to use in a test (a database, a class with a gazillion of dependencies, ...).

Thanks!

Share this post


Link to post
Share on other sites
Advertisement

What do you think about mocks?


I dislike mocking frameworks. They make tests fragile, they're hard to use. They are though sometimes necessary little evils. I'm not counting the auto-generated accessor classes that MSVS will make for you; they're quite useful but less involved.


Do you know what I'm talking about? What's your opinion about this?
[/quote]

Sure, but it sounds as though the tests are shoddy. If you have a method which has (unrefactorable) side effects, then putting a mock in the place of the side effect helps test that. If the method contract (your expectation) changes then the test should break. This is good.


Do you use mocks? Do you like using mocks as much as possible?
[/quote]

Yes. No. I prefer not needing them, or using manual dependency injection with test components. There are a lot of places in my code where components are glued together. Using those glue points for testing ensures that the code is suitably uncoupled and allows me to run the larger app without integration when doing larger tests.


Do you use mocks as "stubs" or "fakes", or do you use them for expectations of how many times a function is called?
[/quote]

I don't particularly see the difference. Sometimes mocks need to provide feedback, sometimes they just need to signal that they've been called. My coding style tends to need them mostly for detecting that side effects have been caused.


Do you write your unit tests to test the result, or the implementation?
[/quote]

They're unit tests. By definition they should ignore the implementation and test the black box that is the unit you're testing. You send in a bunch of inputs and verify outputs including side effects. How can you tell if a method made some web call as a side effect (without making a web server and parsing out logs)? Mock it out. How can you provide some input to the method when that input comes from a database (without installing and populating a database)? Mock it out.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!