Skip to content

How to get DbContext when unit testing with TestServer #1165

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

Closed
CraigSelbert opened this issue Dec 10, 2015 · 4 comments
Closed

How to get DbContext when unit testing with TestServer #1165

CraigSelbert opened this issue Dec 10, 2015 · 4 comments

Comments

@CraigSelbert
Copy link

This is more of a question that an issue but I am not sure where to ask so I am starting here.

When writing unit tests using xUnit and the TestServer how do you get ahold of the database context that you inject into the controller? The services that are being registered during startup are not available using CallContextServiceLocator.Locator.ServiceProvider.GetService.

So when I tried using CallContextServiceLocator.Locator.ServiceProvider.GetService<TestContext>(); it always resolves to null. I know I can set up my tests other ways to make this work but if I could get ahold of the registered services and manipulate them before they where injected that would be ideal.

If this is the wrong place for this question that is fine just please point me in the right direction. I have posted this question on Stack Overflow and I have a working repo

@Eilon
Copy link
Contributor

Eilon commented Dec 10, 2015

@rynowak @dougbu any thoughts on this?

@davidfowl
Copy link
Member

We should really expose the ISP from the TestServer /cc @Tratcher

@CraigSelbert your only option today is to store the ISP in a static and use that from the test

@Tratcher
Copy link
Member

This issue was moved to aspnet/Hosting#519

@CraigSelbert
Copy link
Author

Thanks for the information

jkotalik pushed a commit that referenced this issue Nov 1, 2018
…-master

[automated] Merge branch 'release/2.2' => 'master'
@ghost ghost locked as resolved and limited conversation to collaborators Dec 4, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants