locked
Net Core: Dependency Injection with Repository, Services, and other items when using XUnit RRS feed

  • Question

  • User-1058242892 posted

    I am trying to conduct Dependency Injection in Xunit test for an App Service.

    For some reason, the original application works and can call Customer App Service. However, it cannot call within Xunit. Below is Testserver working using Startup and Configuration from original application.
    We are receiving error below:


    > Message: The following constructor parameters did not have matching fixture data: ICustomerAppService customerAppService

    A good reference is to use answer here from Joshua Duxbury maybe with Fixture class :

    https://stackoverflow.com/questions/37724738/how-to-unit-test-asp-net-core-application-with-constructor-dependency-injection

    or an answer from Mohsen Esmailpour:

    https://stackoverflow.com/questions/50921675/dependency-injection-in-xunit-project/50922017#50922017


    Ideal goal is to run the original application program startup/configuration, and use any dependency injection that was in startup.


    namespace Testing.IntegrationTests
    {
    public class CustomerAppServiceTest
    {
        public DBContext context;
        public ICustomerAppService customerAppService;

    public CustomerAppServiceTest(ICustomerAppService customerAppService)
    {
        this.customerAppService = customerAppService;
    }

    [Fact]
    public async Task Get_CustomerById_Are_Equal()
    {
        var options = new DbContextOptionsBuilder<TestContext>()
        .UseInMemoryDatabase(databaseName: "TestDatabase")
        .Options;
        context = new DbContext(options);

    TestServer _server = new TestServer(new WebHostBuilder()
        .UseContentRoot("C:\\Test\\WebAPI")
        .UseEnvironment("Development")
        .UseConfiguration(new ConfigurationBuilder()
        .SetBasePath("C:\\Test\\WebAPI")
        .AddJsonFile("appsettings.json")
        .Build()).UseStartup<Startup>());

        context.Customer.Add(new Customer { CustomerId = 2, CustomerCode = "123", CustomerName = "ABC" });
        context.SaveChanges();

        var customerDto = await customerAppService.GetCustomerById(2);

        Assert.Equal("123", customerDto.CustomerCode);
    }
    }
    }

    I am receiving this error:

    > Message: The following constructor parameters did not have matching fixture data: ICustomerAppService customerAppService

    Need to use Dependency injection in testing just like real application.
    Original application does this. Do not want Moq answer or answer which bypasses only by straight controller.

    Note: ICustomerAppService has dependency on ICustomerRepository which is also injected in Startup class, and Automapper dependencies. This is why calling the whole startup class.

    Saturday, August 3, 2019 3:29 AM

All replies

  • User1120430333 posted

    I am receiving this error:
    > Message: The following constructor parameters did not have matching fixture data: ICustomerAppService customerAppService

    To me,  what you need is a mocking framework to DI a mocked object for ICustomerAppService.

    Your other option is that somehow on the Actual, meaning the actual test , you make a method that is going to create an instance of the object to be DI.

    This code something that is a rough guess, since I am not developing the test, but I have used the technique. 

    public void SomeTestMethod()
    {
        // call a  method that going to use ICustomerAppService
        CustomerAppServiceTestActual(new ICustomerAppService ) // using the 'new' on ICustomerAppService  instances CustomerAppService into an object to be DI when passed on a constrctor 
        
    }

    Or you can make some setup method that is run doing the test that instances the object and returns the instanced object.

    However, I don't know what your goal is, becuase you can do this for an integration or a functional test, but it won't hold up to the principles of unit test. I fear, becuase you are actually using the object for real, a real instance of the object.

    Saturday, August 3, 2019 2:01 PM