On Saturday, 9 May 2020 09:57:50 UTC+2, Amarjeet Anand wrote:
>
> While writing unit test, what should be the preferred way to mock an 
> external(database, http...) call, *function based approach* or *interface 
> based approach?*
>
> I don't understand when to use function way and when to use interface way. 
> Which one is more idiomatic Golang way if I am writing my web server from 
> scratch?
>

 
Use whatever fits your need. I doubt there is a generic
answer to this question. If it is clear, explicit, readable 
and understandable then it is "idiomatic" no matter what
technique/pattern you use.

Personally I dislike creating an interface just to allow testing
and prefer global functions: I'm thinking of the test as a
bit "secondary" and if the "real" code has only one
implementation of an interface this feels strange. An interface
signals: "Supply whatever implementation fits your need!"
But if there ever is just one "real" implementation this type
of announcement is not clear, explicit or especially
understandable. (Don't get me wrong, I think writing testable
code is important, but clear, understandable code is more
important.)

V.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/d727d1e7-c1ba-4a7c-b382-5be12c7469df%40googlegroups.com.

Reply via email to