2

I cant't find enough information about how to test controllers and classes in asp.net core, when dependency injection is needed.

Using nunit, how to test a class like this:

public class EventServices : Service<EventBase>, IEventServices
{
    private readonly IMemoryCache memCache;
    private readonly UserManager<ApplicationUser> userManager;
    private readonly IHttpContextAccessor accessor;

    public EventServices(
        IRepository<Evento> repository,
        IMemoryCache memCache,
        UserManager<ApplicationUser> userManager,
        IHttpContextAccessor accessor
     ) : base(repository)
    {
        this.memCache = memCache;
        this.userManager = userManager;
        this.accessor = accessor;

    }

    public IQueryable<Evento> MyMethod(string message)
    {
     ....

This classe is registered in Startup class:

services.AddScoped<IEventServices, EventServices>();

like other classes in the constructor.

1
  • 1
    abstract all necessary dependencies so that they can be easily mocked and injected in the unit test. Show an minimal reproducible example of the method under test and the desired behavior. Commented Mar 20, 2017 at 13:57

1 Answer 1

4

How that service is configured at runtime in your ASP.NET Core app has nothing to do with how you would test it. To test it, you should just new it up and pass it its dependencies. If they're tightly coupled to infrastructure (web server, databases, etc) then mock them using a tool like Moq. Remember that unit tests should only be testing your code, not all of your dependencies.

Now, if you want to test how ASP.NET Core works with your service and your DI configuration, that's a different kind of test. For that, you are using an integration or functional test. There's good documentation on how to do just this using ASP.NET Core and the TestServer type here: https://learn.microsoft.com/en-us/aspnet/core/mvc/controllers/testing and https://learn.microsoft.com/en-us/aspnet/core/testing/integration-testing

Basically, you can use TestServer to host your app in memory, and you can tweak how you configure your app using Startup so that it's easier to test (for instance, use an InMemory database instead of your real database). Then you can make requests and get back responses directly from your tests, but all in memory and very fast. They're not unit tests, but they're almost as fast and may do what you're looking for.

The examples are all using xUnit, which is very similar to NUnit. You may find it easier if you just switch to xUnit, since that's what all of the samples for ASP.NET Core use, but if you're committed to NUnit, the syntax changes are minimal.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.