Mocking out calls in standard library

190 views
Skip to first unread message

Inian Vasanth

unread,
Mar 31, 2023, 9:24:14 AM3/31/23
to golang-nuts
Hello Community,

I'm testing out some code for increasing code coverage of our repository, i.e. to maximum level possible. One of the methods I'm dealing with are few methods in standard library (ssh, sftp) that need to be modified for custom behavior.

See Goplay snippet here - https://go.dev/play/p/DGYUqgEKKXh

Basically, I want to modify the type definition of `sshDialer` and `sftpNewClient` to return an interface that is implemented by the native type. If I could do this,  I would generate a mock struct satisfying the same interface and then perform our tests. 

I know mocking standard libraries are not the best of choice, but this is just to get familiar with the language and an experiment with it. 

Also would appreciate other ways to solve the same problem. Thanks

Jason E. Aten

unread,
Apr 1, 2023, 12:20:41 AM4/1/23
to golang-nuts
I would just start a server on 127.0.0.1 and have it provide the expected responses. This is simple and straightforward, and gives a much better test; end-to-end.

The less mocking the better from my point of view. 

If you have to, you would simply wrap the existing structs, creating a higher layer that abstracts. Instead of going below, go above.

Jason E. Aten

unread,
Apr 1, 2023, 12:28:25 AM4/1/23
to golang-nuts
I hasten to add, my library sshego makes it pretty easy to spin up and down an sshd. e.g.

https://github.com/glycerine/sshego/blob/master/server_test.go
Reply all
Reply to author
Forward
0 new messages