DEV Community

souilmimohamed
souilmimohamed

Posted on

C# Xunit Unit testing Withwith Dependency Injection

in A dote net core 6 API project, i use Xunit for unit testing, i'm trying to test a method inside a service ICrpForcastsService that i implement in a partial class CrpForcastsService

public class ForcastTester: IClassFixture<InjectionFixture>
{
    private readonly InjectionFixture injection;
    private readonly Psz.Core.Identity.Models.UserModel _user;
    private readonly ICrpForcastsService _crpForcastsService;
    public ForcastTester(InjectionFixture injection, ICrpForcastsService crpForcastsService)
    {
        this.injection = injection;
        var helpers = new TestingHelpers();
        _user = helpers.GetDefaultTestUser();
        _crpForcastsService = crpForcastsService;
    }
    [Fact]
    public void TestGetForecasts()
    {
        #region Arrange
        var expected = typeof(Psz.Core.Common.Models.ResponseModel<IEnumerable<Psz.Core.CRP.Models.Forcasts.ForcastHeaderModel>>);
        #endregion
        #region Act
        var result = _crpForcastsService.GetForcasts(_user);
        #endregion
        #region Assert
        Assert.NotNull(result);
        Assert.NotNull(result.Body);
        Assert.True(result.Success);
        Assert.IsType(expected, result);
        Assert.True(result.Body.Count() > 0);
        #endregion
    }
}
Enter fullscreen mode Exit fullscreen mode

and i made a fixture class to initiate the startup with a test server like so:

public class InjectionFixture
{
    public readonly TestServer server;
    private readonly HttpClient client;

    public InjectionFixture()
    {
        server = new TestServer(new WebHostBuilder().UseStartup<Startup>());
        client = server.CreateClient();
    }
    public IServiceProvider ServiceProvider => server.Host.Services;

    public void Dispose()
    {
        Dispose(true);
    }
    protected virtual void Dispose(bool disposing)
    {
        if(disposing)
        {
            server.Dispose();
            client.Dispose();
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

and the Startup class where i set the webHsotBuilder and add the service that i'm testting:

public class Startup
{
    public Startup()
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
            .AddEnvironmentVariables();
        Configuration = builder.Build();
    }
    public IConfigurationRoot Configuration { get; }
    public virtual void ConfigureServices(IServiceCollection services)
    {
        services.AddScoped<ICrpForcastsService, CrpForcastsService>();
        services.AddControllers();
    }
    public virtual void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
    }
}
Enter fullscreen mode Exit fullscreen mode

when i rn the test i get this Error:

(No service for type 'Microsoft.AspNetCore.Builder.IApplicationBuilder' has been registered.) (The following constructor parameters did not have matching fixture data: ICrpForcastsService crpForcastsService)
Enter fullscreen mode Exit fullscreen mode

what i'm i missing ??

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay