Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Mayara Ferreira 4 posts 94 karma points
    Mar 22, 2023 @ 14:54
    Mayara Ferreira
    0

    How to Mock IUmbracoDatabaseFactory?

    Hi everyone. Hope you are doing well.

    I am using IUmbracoDatabaseFactory for checking the database health of the application and I would like to add unit tests to it.

    How do I mock the IUmbracoDatabaseFactory configuration, please? The test example code:

        public virtual SampleHealthController healthController => new SampleHealthController(Mock.Of<ILogger<SampleHealthController>>(), Mock.Of<IUmbracoDatabaseFactory>()
    
        [Test]
        public void WhenCheckDB_ThenReturnDatabaseUp()
        {            
            var result = healthController.CheckDB();
            Assert.AreEqual("Up", result);
        }
    
  • Mayara Ferreira 4 posts 94 karma points
    Mar 22, 2023 @ 19:13
    Mayara Ferreira
    100

    Just sharing that a solution was found:

    var iLoggerMock = new Mock<ILogger<HealthController>>();
    var databaseFactoryMock = new Mock<IUmbracoDatabaseFactory>();
    
    //Simulates the database Up
    databaseFactoryMock.Setup(x => x.CanConnect).Returns(true);
    
    var healthController = new HealthController(iLoggerMock.Object, databaseFactoryMock.Object);
    
    var result = healthController.CheckDB();
    
    Assert.AreEqual("Up", result);
    
Please Sign in or register to post replies

Write your reply to:

Draft