Skip to content
This repository was archived by the owner on Dec 19, 2018. It is now read-only.

How to get DbContext when unit testing with TestServer #519

Closed
Tratcher opened this issue Dec 10, 2015 · 6 comments
Closed

How to get DbContext when unit testing with TestServer #519

Tratcher opened this issue Dec 10, 2015 · 6 comments
Assignees
Milestone

Comments

@Tratcher
Copy link
Member

From @CraigSelbert on December 10, 2015 1:36

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

Copied from original issue: dotnet/aspnetcore#1165

@Tratcher
Copy link
Member Author

From @Eilon on December 10, 2015 17:41

@rynowak @dougbu any thoughts on this?

@Tratcher
Copy link
Member Author

From @davidfowl on December 10, 2015 17:46

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

@davidfowl
Copy link
Member

Related to #518

@Tratcher
Copy link
Member Author

TestServer.Services?

@JunTaoLuo
Copy link
Contributor

Application services, including injected DBContext's, can now be accessed from TestServer.Application.Services. cc @CraigSelbert

@CraigSelbert
Copy link

@JunTaoLuo - Thanks for doing this, I will change to use this code base again.

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

No branches or pull requests

4 participants