vendredi 22 juin 2018

Mocking struct fields' methods in golang

Assuming I'm using an external package implementing structs A and B, struct B implements method func (b *B) C() []byte

My code uses this library by calling a.B.C() and using its value, I've already researched mocking in Go, all I was able to find was interfacing method, by creating an interface that the object I want to mock implements and include all the methods I use in my code in that interface, I can create a mocked object that complies with the interface and return my desired values.

However, since struct A is in an external package, I do not have access to change its implementation to use an interface matching B, nor do I have access to the unexported field behind C()

My question is, given the above scenario, is there a way in Golang to mock method C() without having to hardwire the method that calls a.B.C() to check for testing hooks (Like this)?

Extra lore in case there is a more specific solution to the exact original problem (to not cause an xyproblem):

I'm using Valyala's fasthttp library in a small project of mine, however since multiple parts of the code need a fasthttp.RequestCtx to function, I needed to mock parts of it, such as ctx.Request.Header.ContentType() and ctx.Request.Body()

Aucun commentaire:

Enregistrer un commentaire