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 ??

Top comments (0)