Skip to content

How to get DbContext when unit testing with TestServer #1165

Closed
@CraigSelbert

Description

@CraigSelbert

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions