Could you please explain what is a correct way of organizing unit tests? For instance if would like to mock my struct dependencies I need to create a mock dependency which "implements" some interface.
Where should I create this mock? Should it be created in the same test file? But then what if I will need it in another test? Go doesn't allow to define struct with a same name (e.g. UserServiceMock) in 2 different files of the same package. Then what is the best place to define this mock struct?
And another question. Should I implement this kind of mocks by myself or there are some libraries / tools which allow me to do it?
I store my mocks in a mock package so I can call them from different test packages and use that package name in my tests as an indication that I am mocking a dependency. For example:
mock.UserService
You could create a generator or use GoMock
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With