How to test "MemoryCache" ..

6 posts, 0 answers
  1. Bibby
    Bibby avatar
    3 posts
    Member since:
    Jan 2013

    Posted 20 Aug 2013 Link to this post

    Hi..
    I use the JustMock first time. I have no idea to test the method below.
    Anybody could tell me how to do it.
    Thanks.

    public static class CacheHelper
    {
       public static void AddCacheValue(string key, object obj, DateTime dt)
       {
           var policy = new CacheItemPolicy();
           policy.AbsoluteExpiration = dt;
           MemoryCache.Default.Set(key, obj, policy);
       }
    }
  2. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 21 Aug 2013 Link to this post

    Hi Bibby,

    To test the functionality of this unit, I would go for asserting if the methods inside have been called with the expected arguments correct number of times. This can be done like this:
    [TestMethod]
    public void AddCacheValue_OnExecute_InsideFunctionsMustBeCalled()
    {
        var myDate = new DateTime(10, 10, 10);
        var myKey = "testKey";
     
        // Arrange
        var cacheMock = Mock.Create<CacheItemPolicy>();
     
        Mock.ArrangeSet(() => cacheMock.AbsoluteExpiration = myDate).IgnoreInstance().OccursOnce();
        Mock.Arrange(() => MemoryCache.Default.Set(myKey, Arg.AnyObject, Arg.IsAny<CacheItemPolicy>(), null)).OccursOnce();
     
        // Act
        CacheHelper.AddCacheValue(myKey, new Object(), myDate);
     
        // Assert
        Mock.Assert(cacheMock);
    }

    Another approach is to use the DoInstead() method, like this:
    [TestMethod]
    public void AddCacheValue_OnExecute_InsideFunctionsMustBeCalled_UsingDoInstead()
    {
        var myDate = new DateTime(10, 10, 10);
        var myKey = "testKey";
        bool isAbsoluteExpirationCalled = false;
        bool isSetCalled = false;
     
        // Arrange
        var cacheMock = Mock.Create<CacheItemPolicy>();
     
        Mock.ArrangeSet(() => cacheMock.AbsoluteExpiration = myDate).IgnoreInstance().DoInstead(() => isAbsoluteExpirationCalled = true);
        Mock.Arrange(() => MemoryCache.Default.Set(myKey, Arg.AnyObject, Arg.IsAny<CacheItemPolicy>(), null)).DoInstead(() => isSetCalled = true);
     
        // Act
        CacheHelper.AddCacheValue(myKey, new Object(), myDate);
     
        // Assert
        Assert.IsTrue(isAbsoluteExpirationCalled);
        Assert.IsTrue(isSetCalled);
    }

    I hope this helps. Let me know if I can assist you with anything else.

    Regards,
    Kaloyan
    Telerik
    Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
  3. DevCraft R3 2016 release webinar banner
  4. Bibby
    Bibby avatar
    3 posts
    Member since:
    Jan 2013

    Posted 21 Aug 2013 Link to this post

    Thank you for your anser. I try to use the first test, but it's not current.

    [TestMethod]
    public void AddCacheValue_OnExecute_InsideFunctionsMustBeCalled()
    {
        var myDate = new DateTime(10, 10, 10);
        var myKey = "testKey";
      
        // Arrange
        var cacheMock = Mock.Create<CacheItemPolicy>();
      
        Mock.ArrangeSet(() => cacheMock.AbsoluteExpiration = myDate).IgnoreInstance().OccursOnce();
        Mock.Arrange(() => MemoryCache.Default.Set(myKey, Arg.AnyObject, Arg.IsAny<CacheItemPolicy>(), null)).OccursOnce();
      
        // Act
        CacheHelper.AddCacheValue(myKey, new Object(), myDate);
      
        // Assert
        Mock.Assert(cacheMock);
    }

    I comment the code.

    public static void AddCacheValue(string key, object obj, DateTime dt)
    {
        var policy = new CacheItemPolicy();
        //policy.AbsoluteExpiration = dt;
        MemoryCache.Default.Set(key, obj, policy);
    }

    The test is also pass. I don't know why it is. Sorry to disturb you again. Thanks.
  5. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 22 Aug 2013 Link to this post

    Hi again Bibby,

    Thank you for reporting this. It seems as a bug in JustMock. I will add it into our backlog in order to provide a fix for it in one of our future releases.

    Until the fix is out, you can stick to the second test, which is behaving as expected.

    I hope this works for you.

    P.S. For reporting this, we have granted you some Telerik points.

    Regards,
    Kaloyan
    Telerik
    Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
  6. Bibby
    Bibby avatar
    3 posts
    Member since:
    Jan 2013

    Posted 22 Aug 2013 Link to this post

    I got it. Thanks very much.
    ^______^..
  7. Martin
    Admin
    Martin avatar
    29 posts

    Posted 28 Aug 2013 Link to this post

    Hi Bibby,

    In the next release of JustMock we will include a fix for the issue that you discovered. In your case in order to assert the future arrangement (with IgnoreInstance) you will be able to write the following:
    Mock.AssertSet(() => cacheMock.AbsoluteExpiration = myDate, Args.IgnoreInstance());
    The reason that Mock.Assert(cacheMock) doesn't detect the unfulfilled expectation is that expectations for future mocking arrangements are not bound to any particular instance and thus cannot be verified in this way.

    Kind Regards,
    Martin
    Telerik
    Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
Back to Top
DevCraft R3 2016 release webinar banner