41

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

        }

1 Answer 1

52
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.

5
  • i am sure that i am using the same mock object. can you see the edit.
    – loop
    Commented Jun 25, 2014 at 8:00
  • @loop can you post the definition for InAppMock.LicenseManagerService?
    – dcastro
    Commented Jun 25, 2014 at 8:03
  • It is at the top most. actually LiceneseManagerService is one of the property of InAppMock Class.Basically InAppMock class provide Mock of external dependencies. I have done to use it in multiple test cases.
    – loop
    Commented Jun 25, 2014 at 8:07
  • Then that's your problem. inAppMock.LicenseManagerService.Verify creates a new mock instance every time it is called. You should change the property's implementation to return the same instance every time.
    – dcastro
    Commented Jun 25, 2014 at 8:10
  • Got it dcastro. Thanks it is the problem :). can you update your ans a litle bit. so that I can mark it right.
    – loop
    Commented Jun 25, 2014 at 8:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.