Skip to content
This repository was archived by the owner on Jul 28, 2025. It is now read-only.

Commit 08e1d21

Browse files
test: Improved test to assert cloudevent contents
1 parent dd3e61b commit 08e1d21

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

tests/ServiceLayer.API.Tests/Functions/BSSelectFunctionsTests.cs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Dynamic;
2+
using System.Globalization;
23
using Azure;
34
using Azure.Messaging;
45
using Azure.Messaging.EventGrid;
@@ -7,6 +8,7 @@
78
using Microsoft.Extensions.Logging;
89
using Moq;
910
using ServiceLayer.API.Functions;
11+
using ServiceLayer.API.Shared;
1012
using ServiceLayer.API.Tests.Utils;
1113

1214
namespace ServiceLayer.API.Tests.Functions;
@@ -45,14 +47,34 @@ public async Task CreateEpisodeEvent_ShouldSendEventAndReturnOk_WhenRequestIsVal
4547
// Arrange
4648
var request = _setupRequest.CreateMockHttpRequest(_episode);
4749
var mockResponse = Mock.Of<Response>(r => r.IsError == false);
48-
_mockEventGridPublisherClient.Setup(x => x.SendEventAsync(It.IsAny<CloudEvent>(), It.IsAny<CancellationToken>())).ReturnsAsync(mockResponse);
50+
CloudEvent? capturedEvent = null;
51+
_mockEventGridPublisherClient.Setup(x => x.SendEventAsync(It.IsAny<CloudEvent>(), It.IsAny<CancellationToken>()))
52+
.Callback<CloudEvent, CancellationToken>((ce, _) =>
53+
{
54+
capturedEvent = ce;
55+
})
56+
.ReturnsAsync(mockResponse);
4957

5058
// Act
5159
var response = await _functions.IngressEpisode(request);
5260

5361
// Assert
5462
Assert.IsType<OkResult>(response);
55-
_mockEventGridPublisherClient.Verify(x => x.SendEventAsync(It.IsAny<CloudEvent>(), default), Times.Once());
63+
_mockEventGridPublisherClient.Verify(x =>
64+
x.SendEventAsync(It.Is<CloudEvent>(ce =>
65+
ce.Type == "EpisodeEvent" &&
66+
ce.Source == "ServiceLayer" &&
67+
ce.Data != null
68+
), default), Times.Once());
69+
70+
var data = capturedEvent!.Data!.ToObjectFromJson<CreatePathwayParticipantDto>();
71+
Assert.NotNull(data);
72+
Assert.Equal(new Guid("11111111-1111-1111-1111-111111111113"), data.PathwayTypeId);
73+
Assert.Equal("Breast Screening Routine", data.PathwayTypeName);
74+
Assert.Equal("Breast Screening", data.ScreeningName);
75+
Assert.Equal(_episode.nhs_number, data.NhsNumber);
76+
Assert.Equal(DateOnly.Parse(_episode.date_of_birth, CultureInfo.CurrentCulture), data.DOB);
77+
Assert.Equal($"{_episode.first_given_name} {_episode.family_name}", data.Name);
5678
}
5779

5880
[Fact]

0 commit comments

Comments
 (0)