Skip to content

Commit d501f51

Browse files
committed
🧪test: update unit tests to support Redis cache dependency
- Updated unit tests to inject mocked IDistributedCache after the introduction of distributed caching. - All service tests now use Moq to simulate cache behavior, defaulting to cache miss scenarios to validate repository execution and core business logic without requiring a real Redis instance.
1 parent dd71ace commit d501f51

4 files changed

Lines changed: 16 additions & 4 deletions

File tree

EventFlow-API.Tests/Services/EventServiceTests.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
using AutoMapper;
22
using EventFlow.Core.Models;
33
using EventFlow.Core.Repository.Interfaces;
4+
using Microsoft.Extensions.Caching.Distributed;
45

56
namespace EventFlow_API.Tests.Services;
67

78
public class EventServiceTests
89
{
910
private readonly Mock<IEventRepository> _eventRepoMock;
1011
private readonly Mock<IMapper> _mapperMock;
12+
private readonly Mock<IDistributedCache> _cacheMock;
1113
private readonly EventService _eventService;
1214

1315
public EventServiceTests()
1416
{
1517
_eventRepoMock = new Mock<IEventRepository>();
1618
_mapperMock = new Mock<IMapper>();
17-
_eventService = new EventService(_eventRepoMock.Object, _mapperMock.Object);
19+
_cacheMock = new Mock<IDistributedCache>();
20+
_eventService = new EventService(_eventRepoMock.Object, _mapperMock.Object, _cacheMock.Object);
1821
}
1922

2023
[Fact]

EventFlow-API.Tests/Services/OrganizerServiceTests.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using AutoMapper;
22
using EventFlow.Core.Repository.Interfaces;
3+
using Microsoft.Extensions.Caching.Distributed;
34

45
namespace EventFlow_API.Tests.Services;
56

@@ -9,13 +10,15 @@ public class OrganizerServiceTests
910
private readonly Mock<IOrganizerRepository> _mockOrganizerRepository;
1011
private readonly Mock<IEventRepository> _mockEventRepository;
1112
private readonly Mock<IMapper> _mockMapper;
13+
private readonly Mock<IDistributedCache> _mockCache;
1214

1315
public OrganizerServiceTests()
1416
{
1517
_mockOrganizerRepository = new Mock<IOrganizerRepository>();
1618
_mockEventRepository = new Mock<IEventRepository>();
1719
_mockMapper = new Mock<IMapper>();
18-
_service = new OrganizerService(_mockOrganizerRepository.Object, _mockEventRepository.Object, _mockMapper.Object);
20+
_mockCache = new Mock<IDistributedCache>();
21+
_service = new OrganizerService(_mockOrganizerRepository.Object, _mockEventRepository.Object, _mockMapper.Object, _mockCache.Object);
1922
}
2023

2124
[Fact]

EventFlow-API.Tests/Services/ParticipantServiceTests.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using AutoMapper;
22
using EventFlow.Core.Repository.Interfaces;
33
using EventFlow.Infrastructure.Data;
4+
using Microsoft.Extensions.Caching.Distributed;
45

56
namespace EventFlow_API.Tests.Services;
67

@@ -10,18 +11,20 @@ public class ParticipantServiceTests
1011
private readonly Mock<IParticipantRepository> _mockParticipantRepository;
1112
private readonly Mock<IEventRepository> _mockEventRepository;
1213
private readonly Mock<IMapper> _mockMapper;
14+
private readonly Mock<IDistributedCache> _mockCache;
1315

1416
public ParticipantServiceTests()
1517
{
1618
_mockParticipantRepository = new Mock<IParticipantRepository>();
1719
_mockEventRepository = new Mock<IEventRepository>();
1820
_mockMapper = new Mock<IMapper>();
21+
_mockCache = new Mock<IDistributedCache>();
1922

2023
var options = new DbContextOptionsBuilder<EventFlowContext>()
2124
.UseInMemoryDatabase(databaseName: "TestDatabase")
2225
.Options;
2326

24-
_service = new ParticipantService(_mockParticipantRepository.Object, _mockEventRepository.Object, _mockMapper.Object);
27+
_service = new ParticipantService(_mockParticipantRepository.Object, _mockEventRepository.Object, _mockMapper.Object, _mockCache.Object);
2528
}
2629

2730
[Fact]

EventFlow-API.Tests/Services/SpeakerServiceTests.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using AutoMapper;
22
using EventFlow.Core.Repository.Interfaces;
33
using EventFlow.Infrastructure.Data;
4+
using Microsoft.Extensions.Caching.Distributed;
45

56
namespace EventFlow_API.Tests.Services;
67

@@ -10,18 +11,20 @@ public class SpeakerServiceTests
1011
private readonly Mock<ISpeakerRepository> _mockRepository;
1112
private readonly Mock<IEventRepository> _mockEventRepository;
1213
private readonly Mock<IMapper> _mockMapper;
14+
private readonly Mock<IDistributedCache> _mockCache;
1315

1416
public SpeakerServiceTests()
1517
{
1618
_mockRepository = new Mock<ISpeakerRepository>();
1719
_mockEventRepository = new Mock<IEventRepository>();
1820
_mockMapper = new Mock<IMapper>();
21+
_mockCache = new Mock<IDistributedCache>();
1922

2023
var options = new DbContextOptionsBuilder<EventFlowContext>()
2124
.UseInMemoryDatabase(databaseName: "TestDatabase")
2225
.Options;
2326

24-
_service = new SpeakerService(_mockRepository.Object, _mockEventRepository.Object, _mockMapper.Object);
27+
_service = new SpeakerService(_mockRepository.Object, _mockEventRepository.Object, _mockMapper.Object, _mockCache.Object);
2528
}
2629

2730
[Fact]

0 commit comments

Comments
 (0)