简体   繁体   中英

Verify a method is called or not in Unit Test

I have a unit test I am checking whether a method is called once or not so I attempted this way:-

This is my Mock of ILicenseManagerService and I am passing its object through constructor.

    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;
        }

I am using this mock object to set _licenseManagerService and calling the LoadProductLicenses() in method under test. like this. licences are coming fine.

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

My attempt for verify the call to this method -

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

But when I run my unit test, an exception coming that say method is not invoked at all. Where I am doing wrong ?

EDIT @dacastro I am invoking the same mock here is my unit test.

    [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);
                      

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

By calling the LicenseManagerService property, you're creating a new mock object. Naturally, no invocations have ever been performed on this instance.

You should change this property's implementation to return the same instance every time it is called.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM