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.
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
0
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:
Another approach is to use the DoInstead() method, like this:
I hope this helps. Let me know if I can assist you with anything else.
Regards,
Kaloyan
Telerik
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
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.
I comment the code.
The test is also pass. I don't know why it is. Sorry to disturb you again. Thanks.
[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
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
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
0
Bibby
Top achievements
Rank 1
answered on 22 Aug 2013, 12:58 PM
I got it. Thanks very much.
^______^..
^______^..
0
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:
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
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());Kind Regards,
Martin
Telerik

