Repository navigation
Available packages
Here's a quick overview of each package you can find in the library.
This is the main package that provides all the basic tools to build the 3 components (Scenario, API, Simulator) while being agnostic regarding the testing framework or mocking library.
Basically, the Scenario is an object that will contain all the business context for your test. Therefore it is quite difficult to provide technical helpers for everyone; the only interesting bit is the BaseScenario abstract class that will act as a marker for the API and Simulators.
The API component is an object that represents an API endpoint from the tests's point of view. Under the hood, the object inherits from sas' BaseApi (or LazyApi) which in turn will use .Net's WebApplicationFactory in order to run the API under test in memory. Here's an example of such an object.
Finally, the Simulators are mocks/stubs to your API's infra layer. For example you might want to stub some database access or a third party's REST API communications. Again, you can find an example here.
A basic sample API with tests using the SAS concept and library can be found in the samples directory of the repository.
For a more extensive description of the whole SAS concept and a better overview with complete examples, please see this article (curently in French, English to come very soon).
Provides the BaseSimulator class which will instantiate sas' AbstractSimulator.Instance as an NSubstitute stub. Basically, your simulators will look like this :
public class MyServiceSimulator : BaseSimulator<IService>
{
protected override void Simulate(BaseScenario scenario)
{
// provide your stub here using the base class' property Instance
}
}No need to override and instantiate the Instance property anymore.
This is a package whose main goal is to provide boilerplate to stub HttpClients using simulators and your choice of mocking library. All you have to do is to have your simulator derive from the AbstractHttpClientSimulator base class and implement the IDeferHttpRequestHandling interface. You can find a simple example in the sas.simulators.http.tests test project.
This is mostly to be "mocking library or framework" agnostic and avoid locking users into depending from NSubstitute.
This, on the other hand, offers a full implementation of AbstractHttpClientSimulator in the form of BaseHttpClientSimulator, with NSubstitute providing the stub of the IDeferHttpRequestHandling interface. The entire boilerplate will look like this :
public class MyHttpClientSimulator : BaseHttpClientSimulator<MyHttpClient>
{
protected override void Simulate(BaseScenario scenario)
{
HttpClient.Get(Arg.Is<string>(route => route.StartsWith("items/")))
.Returns(_ =>
{
// stub the behaviour here
});
}
}A more "real-world" example can be found here.
Offers a simple simulator to redirect logs to NUnit's test console. Simply add the TestConsoleLoggerSimulator to the list of simulators when instanciating the API and you're done.
This package provides several extension methods for NFluent-based assertions. Basically, this reduces the technical boilerplate when getting the HttpResponseMessage from the API by avoiding the need to check the status and/or perform some JSON deserialisation manually. The point is really to make the tests more "business-oriented" by removing unnecessary technical stuff.
var response = MyApi.DoStuff(param1, param2);
Check.That(response).IsOk<MyPayload>()
.WhichPayload(payload =>
{
// perform checks and operations on payload which is of MyPayload type.
});or
var response = MyApi.DoStuffWhileServiceIsDown();
Check.That(response).HasStatus(HttpStatusCode.ServiceUnavailable);An example can be found here.
Last but not least, this package provides a way to read configuration from Azure Key Vault in addition to the usual appsettings.json file. This is quite interesting when sensitive information (e.g. credentials, API tokens...) is defined in a secured place and you want to perform integration testing or your API is unable to start when this information is not provided.