Skip to content

Commit

Permalink
added unit tests for the cohort service and modified the existing MA …
Browse files Browse the repository at this point in the history
…tests to use the new concrete class
  • Loading branch information
sscaife committed Sep 9, 2021
1 parent dd90c52 commit 9e03a98
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public async Task Then_The_Endpoint_Is_Called_With_Authentication_Header_And_Dat
};
var httpMessageHandler = MessageHandler.SetupMessageHandlerMock(response, $"{config.BaseUrl}{getTestRequest.GetUrl}", config.Key);
var client = new HttpClient(httpMessageHandler.Object);
var apiClient = new ApiClient(client, config);
var apiClient = new ManageApprenticeshipsApiClient(client, config);

//Act
var actual = await apiClient.Get<List<string>>(getTestRequest);
Expand All @@ -57,7 +57,7 @@ public void Then_If_It_Is_Not_Successful_An_Exception_Is_Thrown()

var httpMessageHandler = MessageHandler.SetupMessageHandlerMock(response, $"{config.BaseUrl}{getTestRequest.GetUrl}", config.Key);
var client = new HttpClient(httpMessageHandler.Object);
var apiClient = new ApiClient(client, config);
var apiClient = new ManageApprenticeshipsApiClient(client, config);

//Act Assert
Assert.ThrowsAsync<HttpRequestException>(() => apiClient.Get<List<string>>(getTestRequest));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DocumentFormat.OpenXml.Office2010.CustomUI;
using Moq;
using NUnit.Framework;
using SFA.DAS.CommitmentsV2.Types;
using SFA.DAS.EmployerFinance.Infrastructure.OuterApiRequests;
using SFA.DAS.EmployerFinance.Infrastructure.OuterApiResponses;
using SFA.DAS.EmployerFinance.Interfaces.OuterApi;
using SFA.DAS.EmployerFinance.Services;

namespace SFA.DAS.EmployerFinance.UnitTests.Services.CohortsServiceTests
{
public class WhenIGetACohortsCount
{
private Mock<IApiClient> _apiClient;
private CohortsService _service;

[SetUp]
public void Setup()
{
_apiClient = new Mock<IApiClient>();
_service = new CohortsService(_apiClient.Object);
}

[Test]
public async Task ThenTheApiIsCalledWithAValidAccountIdAndTheCohortsCountIsReturned()
{
SetupApiClient(1);

var actual = await _service.GetCohortsCount(1);

Assert.AreEqual(1, actual);
}

[Test]
public async Task ThenTheApiIsCalledWithAnInvalidAccountIdAndTheCohortsCountIsZero()
{
SetupApiClient(0, false);

var actual = await _service.GetCohortsCount(0);

Assert.AreEqual(0, actual);
}

private void SetupApiClient(long accountId, bool addItem = true)
{
var items = new List<CohortSummary>();

if (addItem)
{
items = new List<CohortSummary>
{
new CohortSummary
{
AccountId = accountId
}
};
}

_apiClient.Setup(o =>
o.Get<GetCohortsResponse>(
It.Is<GetCohortsRequest>(i => i.GetUrl.Equals($"api/cohorts?accountId={accountId}"))))
.ReturnsAsync(new GetCohortsResponse
{
Cohorts = items
});
}
}
}

0 comments on commit 9e03a98

Please sign in to comment.