agiledragon / gomonkey

gomonkey is a library to make monkey patching in unit tests easy
MIT License
2k stars 179 forks source link

could we mock interface directly? #33

Closed hitzhangjie closed 3 years ago

hitzhangjie commented 3 years ago

Could we mock interface variable directly instead of create a struct implementing interface methods?

If technically we can, how difficult is it? What should we do ?

I think we can dynamiclly create a struct and add methods to it, and use this struct as an instance of type of interface implemention.

agiledragon commented 3 years ago

Let alone whether it is technically feasible. If you define an interface, then at least one class in the system will implement this interface, otherwise, what do you define this interface for? Now that there is an implementation class, we will not need to create that dynamically, right? The implementation class can be reused directly in the test.

hitzhangjie commented 3 years ago

Yes.