gonkey icon indicating copy to clipboard operation
gonkey copied to clipboard

Start mockserver with definition at startup

Open amuttsch opened this issue 5 years ago • 3 comments

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?

amuttsch avatar Nov 25 '19 13:11 amuttsch

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:

  1. Simply make definition and newDefinition function public.
  2. Create a new API to allow pre-setup mocks.

What are your thoughts @luza?

fetinin avatar Nov 27 '19 16:11 fetinin

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.

luza avatar Nov 27 '19 20:11 luza

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: ...

amuttsch avatar Nov 28 '19 12:11 amuttsch