Unit testing your integration - AquaticInformatics/aquarius-sdk-net GitHub Wiki
The Aquarius.Client
assembly has an associated Aquarius.Client.UnitTests
assembly, to test its classes and give confidence that the SDK will work as expected.
But what about your integration that consumes the Aquarius SDK? How do you unit test your integration without requiring a real connection to a real AQTS server?
The Aquarius SDK has been designed to be easily unit-testable, exposing interfaces for the key integration points.
These examples will use the following excellent packages for easy unit testing.:
- NUnit for the base test framework
- NSubstitute for easily mocking out code behind an interface
- AutoFixture for easily creating test data
- FluentAssertions for succinct assertions about expected behaviour
There are alternatives for each package, and you are free to choose something different, but these are the ones we tend to use for our test code at Aquatic Informatics.
This snippet will create a basic mock for the main AquariusClient
class. This mocked class will return a mocked IServiceClient
interface for each of the public API endpoints.
[TestFixture]
public class MyClassTests
{
private IAquariusClient _mockClient;
private IServiceClient _mockPublishClient;
private IServiceClient _mockProvisioningClient;
private IServiceClient _mockAcquisitionClient;
[SetUp]
public void ForEachTest()
{
SetupMockClient();
}
private void SetupMockClient()
{
_mockClient = Substitute.For<IAquariusClient>();
_mockClient.SessionKeepAlive().ReturnsForAnyArgs(new ScopeAction(() => { }));
_mockPublishClient = Substitute.For<IServiceClient>();
_mockProvisioningClient = Substitute.For<IServiceClient>();
_mockAcquisitionClient = Substitute.For<IServiceClient>();
_mockClient.Publish.Returns(_mockPublishClient);
_mockClient.Provisioning.Returns(_mockProvisioningClient);
_mockClient.Acquisition.Returns(_mockAcquisitionClient);
}
...
}
It is recommended to read up on the NSubstitute documentation, but here are a few quick examples of the common test patterns your code might use with these mocked interfaces.
Ask the mock to return a simulated response DTO for an request DTO.
// Simulate a successful "GET /parameters" request on the Provisioning API
// Construct the simulated list however you need
var parameters = new List<Parameter> { ... };
// Have the mock respond with a successful response DTO
_mockProvisioningClient
.Get(Arg.Any<GetParameters>())
.Returns(new ParametersResponse { Results = parameters });
// This will simulate the HTTP request/response sequence
var response = _mockClient.Provisioning.Get(new GetParameters());
// Now make an assertion about the expected results
response.Results.ShouldAllBeEquivalentTo(parameters);
// Alternatively, your code could simply verify that a request was issued
_mockProvisioningClient
.Received(1)
.Get(Arg.Any<GetParameters>());
If you need to test your integrations error handling, you can force a mock to throw a WebServiceException
.
// Force the mock to throw a WebServiceException
_mockProvisioningClient
.Get(Arg.Any<GetParameters>())
.ThrowsForAnyArgs(new WebSeviceException("Not Found"));
// This action should throw
Action action = () => _mockClient.Provisioning.Get(new GetParameters());
// Let's make sure it throws
action.ShouldThrow<WebServiceException>();