This is a migrated thread and some comments may be shown as answers.

How to test "MemoryCache" ..

5 Answers 3751 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Bibby
Top achievements
Rank 1
Bibby asked on 20 Aug 2013, 02:46 PM
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);
   }
}

5 Answers, 1 is accepted

Sort by
0
Kaloyan
Telerik team
answered on 21 Aug 2013, 07:40 AM
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.
0
Bibby
Top achievements
Rank 1
answered on 21 Aug 2013, 04:21 PM
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.
0
Kaloyan
Telerik team
answered on 22 Aug 2013, 07:52 AM
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.
0
Bibby
Top achievements
Rank 1
answered on 22 Aug 2013, 12:58 PM
I got it. Thanks very much.
^______^..
0
Martin
Telerik team
answered on 28 Aug 2013, 10:03 AM
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.
Tags
General Discussions
Asked by
Bibby
Top achievements
Rank 1
Answers by
Kaloyan
Telerik team
Bibby
Top achievements
Rank 1
Martin
Telerik team
Share this question
or