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

How to get a dependency service without starting a service? #194

Closed
timur-han opened this issue Aug 13, 2015 · 7 comments
Closed

How to get a dependency service without starting a service? #194

timur-han opened this issue Aug 13, 2015 · 7 comments

Comments

@timur-han
Copy link

Hi,

Is there a way to get the instance of a dependency service without starting the service? I want to test certain functions using a dependency service but without actually starting the dependent service.

Regards,

Timur

@cprice404
Copy link

@timur87 if I'm understanding you correctly, the best way to do this for testing is implement a mock version of the dependency service. Then you can use the testutils with-app-with-config macro and pass it a list of services that you want it to include, including the mock version of the dependency service instead of the real version.

Would that suffice for your needs?

@timur-han
Copy link
Author

@cprice404, thanks for the prompt answer again.

What I'm asking might be a really basic question :) What I need is an actual instance of the dependency service. Something similar to (services/get-service this :MyProtocol) except without "this" parameter here so that I can call it outside of the start or init functions.

@cprice404
Copy link

@timur87 so this is in test code? With a TK app booted up? Or are you saying you're trying to write tests that test the service without booting a TK app?

@timur-han
Copy link
Author

It's not a test code, I'm working in repl interactively and want to see if the service really does its job. It's a persistency service, and I want to see if the things are really added. For that I need to get an instance of a dependency service, i.e., persistency service. All of this is happening in CIDER nREPL :)

@cprice404
Copy link

Ah, OK, so you do have a full instance of the TK app running then. There's probably a hacky way to do it; you can use this variable:

https://github.com/puppetlabs/trapperkeeper/blob/master/src/puppetlabs/trapperkeeper/core.clj#L122

to get a reference to the TrapperkeeperApp itself, and then use the get-service function from the TrapperkeeperApp protocol to get a reference to a service.

https://github.com/puppetlabs/trapperkeeper/blob/master/src/puppetlabs/trapperkeeper/app.clj#L5

I hate having that global variable in the code but this is what I put it in there for, as a last resort for this kind of situation. For a dev environment it'd probably be preferable to boot the tk-app explicitly in the repl and hold on to your own variable with a reference to the app, and access it through that, if possible.

@cprice404
Copy link

@timur87 did that work for your use case?

@timur-han
Copy link
Author

Yes it did thanks for the prompt answers as always!

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

No branches or pull requests

2 participants