Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start mockserver with definition at startup #35

Open
amuttsch opened this issue Nov 25, 2019 · 3 comments
Open

Start mockserver with definition at startup #35

amuttsch opened this issue Nov 25, 2019 · 3 comments

Comments

@amuttsch
Copy link

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?

@fetinin
Copy link
Contributor

fetinin commented Nov 27, 2019

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?

@luza
Copy link
Contributor

luza commented Nov 27, 2019

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.

@amuttsch
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants