8
rutee07
17d

I feel really dumb for not understanding this but somehow my brain refuses to make sense of it. Using mocks in unit tests that read like this:

Mock: 1 + 1 = 3
Assert: Is 1 + 1 equals to 3?

Or when testing an API and mocking the response to "something" and then testing if it returns "something". I don't get it. I just don't. What did you test? Your typing skills? Your sanity? When does it fail? How is it a test when even if I intentionally break the actual code you are writing the tests for, it still returns "successful" because you kind of programmed it that way?

Call me an idiot but I've been reading about this for a long time now. I see examples online where mocking makes sense and it actually helps test the code but this kind of writing doesn't make sense to me. Some enlightenment would be appreciated.

Comments
  • 5
    Testing the test framework, just to be sure. It's also a good practice to assert that 1 == 1 evaluates to true and 1 == 0 to false.
  • 7
    There are companies where the number of tests is a factor the management has goals for.

    If this is done in an otherwise toxic environment I can see tests like this being created as a sort of F U to bad management.

    But as anything else than to raise the number if tests OR if your building a parser/compiler no.

    You usually do not test built in methods or functionality, you expect the builders of the language/library to do that.
  • 4
    this approach is cheating. It's as good as

    @Test
    public void testFeatureX() {return;}

    The test passes w/o a failure.

    UNLESS your tests are there to test mock validity (i.e. if you are reusing the same mock in multiple tests and decide to change it a little bit for test7 which would break tests 1-8).

    Mocking is useful for replacing parts of the code that are hard to test or do not concern the current test. Like I/O, DB repositories, some providers,... For instance, you might want some provider to return a spy into the application code so that you could verify whether the flow was correct - use a mocked provider for that purpose. Or maybe you're using an integration with a 3rd party service -- use a mocked integration for that purpose returning data structures a real integration would return.

    Mocks, when used incorrectly, are worse than the code not tested. Cheated tests give you a false sense of security and lie you with confidence that your code works properly.
  • 2
    Mocking the api allows you to test that your code calls the api when expected, so it's not so much the returned value that is important (although tests could confirm that the returned data are manipulated as expected) but the fact that the api is called.
  • 0
    Mocks are used to replace external data sources or other classes the class you are testing calls to get data from so that you only test the functionality of the class.

    Ie. Some method queries the DB to get a record and uses one of the columns.

    The columns is a VARCHAR but the values are expected to be numbers. Except someone else fucked up the DB with bad data and changed it to TEST. Or they crashed the DB.

    Without a mock, ur tests fail but it's not your fault.

    Mocks just hold everything else constant so you're testing just what your class is supposed to do
  • 1
    @nibor Depends on your test's goal and the unit you are testing. If you're only testing whether your code issues a call to the API correctly - sure, use a spy and call it a day. But if you need to test whether your code interprets/maps the response correctly and/or uses data from that response in further flows properly - then yes, you do need to mock a response.
Add Comment