-
Notifications
You must be signed in to change notification settings - Fork 0
/
VehicleServiceTests.cs
110 lines (93 loc) · 3.74 KB
/
VehicleServiceTests.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using CachingInmemory.Models;
using CachingInmemory.Services;
using Microsoft.Extensions.Caching.Memory;
using Moq;
using Moq.Protected;
using NUnit.Framework;
using System.Net;
using System.Net.Http;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
namespace CachingInmemory.Tests
{
[TestFixture]
public class VehicleServiceTests
{
private Mock<IMemoryCache> _memoryCacheMock;
private Mock<HttpMessageHandler> _httpMessageHandlerMock;
private HttpClient _httpClient;
private VehicleService _vehicleService;
[SetUp]
public void SetUp()
{
_memoryCacheMock = new Mock<IMemoryCache>();
// Mock the HttpMessageHandler to simulate HttpClient behavior
_httpMessageHandlerMock = new Mock<HttpMessageHandler>();
// Create a new HttpClient with the mocked handler
_httpClient = new HttpClient(_httpMessageHandlerMock.Object);
// Create an instance of VehicleService with the mocked IMemoryCache and HttpClient
_vehicleService = new VehicleService(_memoryCacheMock.Object, _httpClient);
}
[Test]
public async Task GetVehicles_ReturnsVehiclesFromApi_WhenNotCached()
{
// Arrange
List<Vehicle> vehicles = new List<Vehicle>
{
new Vehicle { Id = 1, Name = "Car" },
new Vehicle { Id = 2, Name = "Truck" }
};
var apiResponse = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(JsonSerializer.Serialize(vehicles))
};
// Setup the handler to return the mock response for any request
_httpMessageHandlerMock.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.IsAny<HttpRequestMessage>(),
ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(apiResponse);
// Setup cache to return null (simulate cache miss)
object cacheEntry = null;
_memoryCacheMock.Setup(mc => mc.TryGetValue(It.IsAny<object>(), out cacheEntry)).Returns(false);
// Act
var result = _vehicleService.GetVehicles();
// Assert
Assert.IsNotNull(result);
Assert.AreEqual(2, result.Count);
Assert.AreEqual("Car", result[0].Name);
Assert.AreEqual("Truck", result[1].Name);
// Verify that the Set method was called to cache the result
_memoryCacheMock.Verify(mc => mc.Set(
It.IsAny<object>(),
It.IsAny<object>(),
It.IsAny<MemoryCacheEntryOptions>()),
Times.Once);
}
[Test]
public async Task GetVehicles_ReturnsCachedVehicles_WhenInCache()
{
// Arrange
List<Vehicle> cachedVehicles = new List<Vehicle>
{
new Vehicle { Id = 3, Name = "Motorcycle" }
};
// Setup cache to return cached vehicles
_memoryCacheMock.Setup(mc => mc.TryGetValue(It.IsAny<object>(), out cachedVehicles)).Returns(true);
// Act
var result = _vehicleService.GetVehicles();
// Assert
Assert.IsNotNull(result);
Assert.AreEqual(1, result.Count);
Assert.AreEqual("Motorcycle", result[0].Name);
// Verify that the Set method was never called since the data was cached
_memoryCacheMock.Verify(mc => mc.Set(
It.IsAny<object>(),
It.IsAny<object>(),
It.IsAny<MemoryCacheEntryOptions>()),
Times.Never);
}
}
}