Search code examples
unit-testing.net-coremockingasp.net-core-webapi

Unit test method not executed


I have this test class

[TestClass]
public class AccountTypeTests
{       
    private AccountTypeService _accountTypeService;

    [TestInitialize]
    private void Initialize ()
    {
        var options = new DbContextOptionsBuilder<AccountingDBContext>()
                    .UseInMemoryDatabase(databaseName: "InMemoryDatabase")
                    .Options;
        var mock=new Mock<IDbContextFactory<AccountingDBContext>>();
        mock.Setup(p => p.CreateDbContext()).Returns(() => new AccountingDBContext(options));
         
        _accountTypeService = new AccountTypeService(mock.Object);
    }

    [TestMethod]
    public void AccountTypeIsCreatedWhenGuidIsValid()
    {
    }
}

So that if I'm doing that

    private AccountTypeService _accountTypeService;

    private AccountTypeService Initialize ()
    {
        var options = new DbContextOptionsBuilder<AccountingDBContext>()
                    .UseInMemoryDatabase(databaseName: "InMemoryDatabase")
                    .Options;
        var mock=new Mock<IDbContextFactory<AccountingDBContext>>();
        mock.Setup(p => p.CreateDbContext()).Returns(() => new AccountingDBContext(options));
         
        return _accountTypeService = new AccountTypeService(mock.Object);
    }

    [TestMethod]
    public void AccountTypeIsCreatedWhenGuidIsValid()
    {
        //Arrange
        AccountTypeService _accountTypeService=Initialize();
    }

No error, nothing, but not launched. I have also tried to include the factory in the constructor, but same behavior.

Any idea?


Solution

  • Finally I have tried with xUnit. It works...