Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where do you store reusable mocks?

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?

like image 266
RhinoLarva Avatar asked Mar 11 '17 17:03

RhinoLarva


1 Answers

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

like image 86
Steve C Avatar answered Sep 23 '22 23:09

Steve C