Service Tests - SingletonTheory/SingletonTheory.github.io GitHub Wiki

Test Tool

Repository Tests

Example

UserRepositoryTests found in the SingletonTheory.Services.AuthServices.Tests project.

Service Tests

Example

To test services the services needs to be hosted in the tests. This can be done by way of the HostUtility class which can be found in the SingletonTheory.Web.TestUtilities project under the Hosts folder. You have to initialize this utility in the TestFixtureSetUp section of your test class as follows:

[TestFixtureSetUp]
public void TestFixtureSetUp()
{
	_host = new HostUtility(HTTPClientHelpers.GetAssembliesToLoad(), HTTPClientHelpers.AdminUserName, HTTPClientHelpers.Password);
}

This utility implements IDisposable, so you have to dispose of it in the TestFixtureTearDown section of your test class as follows:

[TestFixtureTearDown]
public void TestFixtureTearDown()
{
	_host.Dispose();
	_host = null;
}

Once you have this setup, the utility exposes the JSONServiceClient for you to make your service calls on as follows:

_host.JsonClient.Post(request);

In all, your Service tests should look something like:

[Test]
public void ShouldActionAndAssert()
{
	// Arrange
	var request = new Request();

	// Act
	var response = _host.JsonClient.Post(request);

	// Assert
	Assert.AreNotEqual(response.Prop1, 12);
}

Naming Conventions

Should Naming Convention

All tests starts with Should and then continues to explain what the specific thing is that is tested. An example is as follows:

ShouldCreateInstanceOfTypeShipperWithIntKey

This test will then follow the Arrange, Act, Assert (AAA) standard which will make an entire example look like:

[Test]
public void ShouldCreateInstanceOfTypeShipperWithIntKey()
{
	// Arrange
	using (SqlProvider provider = new SqlProvider(ConfigSettings.ConnectionString, false))
	{
		// Act
		IShipper shipper = _repository.CreateInstance<IShipper>(provider, typeof(Shipper<>));

		// Assert
		Assert.IsNotNull(shipper);
	}
}

Rules of Testing

  1. Every test should have only one Act.
⚠️ **GitHub.com Fallback** ⚠️