【问题标题】:Verify a method is called or not in Unit Test验证单元测试中是否调用了方法
【发布时间】:2014-06-25 07:44:08
【问题描述】:

我有一个单元测试我正在检查一个方法是否被调用一次,所以我尝试了这种方式:-

这是我对ILicenseManagerService 的模拟,我正在通过构造函数传递它的对象。

    public Mock<ILicenseManagerService> LicenseManagerService { get { return SetLicenseManagerServiceMock(); } }

        private Mock<ILicenseManagerService> SetLicenseManagerServiceMock()
        {
            var licencemangerservicemock = new Mock<ILicenseManagerService>();
            licencemangerservicemock.Setup(m => m.LoadProductLicenses()).Returns(ListOfProductLicense).Verifiable();

            return licencemangerservicemock;
        }

        public static async Task<IEnumerable<IProductLicense>> ListOfProductLicense()
        {
            var datetimeoffset = new DateTimeOffset(DateTime.Now);

            var lst = new List<IProductLicense>
            {
                GetProductLicense(true, datetimeoffset, false, "1"),
                GetProductLicense(true, datetimeoffset, false, "2"),
                GetProductLicense(true, datetimeoffset, true, "3")
            };

            return lst;
        }

我正在使用这个模拟对象来设置_licenseManagerService 并在被测方法中调用LoadProductLicenses()。像这样。 许可证即将发放。

    var licenses = (await _licenseManagerService.LoadProductLicenses()).ToList();

我尝试验证对此方法的调用 -

     LicenseManagerService.Verify(m => m.LoadProductLicenses(),Times.Once);

但是当我运行我的单元测试时,根本不会调用 say 方法的异常。 我哪里做错了?

编辑 @dacastro 我在这里调用相同的模拟是我的单元测试。

    [TestMethod]
        [TestCategory("InApp-InAppStore")]
        public async Task return_products_from_web_when_cache_is_empty()
        {
            // this class basically for setting up external dependencies
            // Like - LicenceManagerService in context, i am using this mock only no new mock.
            var inAppMock = new InAppMock ();                  


            // object of Class under test- I used static method for passing external         
            //services for easy to change 
            var inAppStore = StaticMethods.GetInAppStore(inAppMock);

            // method is called in this method
            var result = await inAppStore.LoadProductsFromCacheOrWeb();

            // like you can see using the same inAppMock object and same LicenseManagerService
            inAppMock.LicenseManagerService.Verify(m => m.LoadProductLicenses(),Times.Once);
                      

        }

【问题讨论】:

    标签: c# unit-testing tdd moq


    【解决方案1】:
    LicenseManagerService.Verify(m => m.LoadProductLicenses(),Times.Once);
    

    通过调用LicenseManagerService 属性,您正在创建一个 模拟对象。自然,从未在此实例上执行过任何调用。

    您应该更改此属性的实现以在每次调用时返回相同的实例。

    【讨论】:

    • 我确信我使用的是同一个模拟对象。你能看到编辑吗?
    • @loop 你能发布InAppMock.LicenseManagerService的定义吗?
    • 它在最顶端。实际上 LiceneseManagerService 是 InAppMock 类的属性之一。基本上 InAppMock 类提供外部依赖的 Mock。我已经在多个测试用例中使用它。
    • 那是你的问题。 inAppMock.LicenseManagerService.Verify 每次调用时都会创建一个新的模拟实例。您应该更改属性的实现以每次都返回相同的实例。
    • 知道了 dcastro。谢谢这是问题:)。你能稍微更新一下你的ans吗?这样我就可以正确标记它。
    猜你喜欢
    • 2018-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-18
    相关资源
    最近更新 更多