I'm writing unit tests for a class that makes use of a general utilities module. In general I want to mock all the dependencies of the class, but mocking some of the utility functions feels weird. Some of them are simple functions for dealing with file paths, string manipulation, etc, and it seems like an implementation detail that the class under test uses this utility to implement its public API. If the code was refactored to not need that utility function, I wouldn't care and would prefer that the test not fail.The problem is that some of the utilities functions depend on the filesystem, and obviously need to be mocked. So I'm considering eitherLeaving the utilities module as-is and mocking the filesystem moduleOr using most of the utilities module but mocking the individual functions that have dependencies on the filesystemWhat have other people done in this scenario?
Mocking a utilities module for unit testing?
unit testing
It depends what you consider a unit. Some people think a unit to be tested should be the tiniest part they can reduce matters to - ie just the function under test with everything it depends on mocked away. I think this is just taking things too far towards ideology of some utopian dream of unit testing.A unit should be a simple part of your program that is easy to test, without being too large that it becomes a mini-integration test. For many people this means the class, and many (including Martin Fowler) would argue that some dependant classes be included in that definition. So a utility class of file IO wrappers would be perfectly fine to consider as part of the unit being tested.So its up to you to decide how granular to take your system for testing. If you think your filesystem module works fine (and has been tested itself perhaps) then simply include it and take the (very slight) risk that it might cause your function to fail testing (which isn't a bad thing as you can then fix it - which is what testing is all about). This may mean you spend far less time writing tests than you do fixing any potential bugs found while testing your function.