R – Mocking problem, public function calling private function

mockingprivate-functionsrhino-mocks

I’m having a mocking problem. I know only public methods should be mocked. But what do I do when a private method is called in a public method, and this private method is getting data from a file? I want to mock this private method so I could go on in with the test of the public method.

I could make this public to make the test work, but that would make no sense since it is private for a reason. I could also move everything into another class and make the functions public there, but the reference to the object in the main class should either way be private.

I'm using Rhino Mocks.

Thanks for any help 🙂

Best Answer

If you put the functionality that does the file reading into a class e.g

FileReader : IFileReader

Then pass in IFileReader as an arg to the constructor. you can then mock that