gonkey
gonkey copied to clipboard
Start mockserver with definition at startup
Currently, mocks can only be defined on a per test basis. We have a service that connects to an external endpoint (elasticSearch) on startup, before the http server is started. Our service cannot start as m := mocks.NewNop("elastic")
returns 204 for all requests on this mock endpoint and it thinks that elasticsearch is not available.
Is it possible to start a mock server with a definition before the actual tests run?
I'm afraid that that's currently not possible.
Mock has SetDefinition()
method that accepts mock definition, however definition
struct is private. It actually doesn't make sense that public method accepts private structs. I see 2 solutions here:
- Simply make
definition
andnewDefinition
function public. - Create a new API to allow pre-setup mocks.
What are your thoughts @luza?
I think you could initialise a mock in this way (not an elegant, though):
m := mocks.NewNop("elastic")
def := map[string]interface{}{
"elastic": map[string]interface{}{
"strategy": // ... and so on in the way you write yaml
},
}
loader := mocks.NewLoader(m)
err := loader.Load(def)
However I also like the idea to open the mock initialisation API, i.e. not just definition
and newDefinition
but also interfaces verifier
, strategy
and bunch of builders newXXX()
for standard strategies and verifiers.
Thanks for the workaround @luza, I'll have a look at it.
As for a proper solution I'd suggest sth like mocks.NewGlobal("some_mocks.yml")
for an easy and similar semantic where the yaml looks sth. like this:
elasticSearch:
strategy: methodVary
methods: ...
someOtherMock:
strategy: ...