-
Notifications
You must be signed in to change notification settings - Fork 0
/
CachingServiceTests2.cs
70 lines (59 loc) · 2.87 KB
/
CachingServiceTests2.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using Microsoft.Extensions.Caching.Memory;
using Moq;
using NUnit.Framework;
using System;
namespace CachingServiceTests.Tests
{
[TestFixture]
public class CachingServiceTests
{
private CachingService _cachingService;
private Mock<ICacheProvider> _cacheProviderMock;
private Mock<ICacheEntry> _cacheEntryMock;
[SetUp]
public void Setup()
{
_cacheProviderMock = new Mock<ICacheProvider>();
_cachingService = new CachingService(_cacheProviderMock.Object);
_cacheEntryMock = new Mock<ICacheEntry>();
}
[Test]
public void GetOrAdd_ShouldUseCacheEntryCorrectly()
{
// Arrange
var key = "test_key";
var item = "test_item";
_cacheProviderMock
.Setup(p => p.GetOrCreate(It.IsAny<string>(), It.IsAny<MemoryCacheEntryOptions>(), It.IsAny<Func<ICacheEntry, object>>()))
.Returns<string, MemoryCacheEntryOptions, Func<ICacheEntry, object>>((k, options, factory) => factory(_cacheEntryMock.Object));
// Mocking relevant properties of ICacheEntry
_cacheEntryMock.SetupProperty(e => e.AbsoluteExpirationRelativeToNow, TimeSpan.FromMinutes(10));
// Act
var result = _cachingService.GetOrAdd(key, entry => item);
// Assert
Assert.AreEqual(item, result);
_cacheProviderMock.Verify(p => p.GetOrCreate(It.IsAny<string>(), It.IsAny<MemoryCacheEntryOptions>(), It.IsAny<Func<ICacheEntry, object>>()), Times.Once);
Assert.AreEqual(TimeSpan.FromMinutes(10), _cacheEntryMock.Object.AbsoluteExpirationRelativeToNow);
}
[Test]
public void GetOrAdd_ShouldRegisterPostEvictionCallback()
{
// Arrange
var key = "test_key";
var item = "test_item";
// Setup the mock to call the cache factory when GetOrCreate is invoked
_cacheProviderMock
.Setup(p => p.GetOrCreate(It.IsAny<string>(), It.IsAny<MemoryCacheEntryOptions>(), It.IsAny<Func<ICacheEntry, object>>()))
.Returns<string, MemoryCacheEntryOptions, Func<ICacheEntry, object>>((k, options, factory) => factory(_cacheEntryMock.Object));
// Mock the PostEvictionCallbacks collection and behavior
var postEvictionCallbackRegistrationMock = new Mock<PostEvictionCallbackRegistration>();
var evictionCallbacks = new System.Collections.Generic.List<PostEvictionCallbackRegistration>();
_cacheEntryMock.Setup(e => e.PostEvictionCallbacks).Returns(evictionCallbacks);
// Act
var result = _cachingService.GetOrAdd(key, entry => item);
// Assert
Assert.AreEqual(item, result);
Assert.IsNotEmpty(evictionCallbacks); // Verifies that a callback was registered
}
}
}