OpenSourced: JustFakeIt

Introducing JustFakeIt. A Http Mocking Library

JustFakeIt is designed to help you write better tests by mocking away any 3rd party HTTP services your application might depend on.  JustFakeIt does this by acting as a HTTP server which is hosted within your unit test process and which you can specify mock endpoints and responses against. Because it’s hosted In Process, it’s really fast and takes very little effort to use.

Why would I want to use this?

We’re used to writing tests that mock HTTP clients, like the really basic test below which mocks a CustomerApiClient

public void OrderService_WhenFetchingAValidOrder_CustomerDetailsAreReturned()
{
   OrderService orderService = new OrderService(GetMockFor<ICustomerApiClient>());
   GetMockFor<ICustomerApiClient>().Setup(c => c.GetCustomerById(1))
                                   .Returns(new Customer { Id = 1, Name = "Jo Bloggs" });
   Order order = orderService.GetOrder(123);
   GetMockFor<ICustomerApiClient>().Verify(c => c.GetCustomerById(1));
   Assert.That(order.Customer.Id.Equals(1));
}

The problem with tests like these are that by mocking away the CustomerApiClient, we don’t actually test it.  What about the logic that goes on inside the client? Typically we might write a suite of integration tests against the CustomerApiClient, but integration tests take a fair amount of time and effort to write compared to a unit test, and we might not cover our core usage scenarios (integration tests against external dependencies are usually very isolated) JustFakeIt helps in this situation by allowing us to write a unit test that covers the whole of the API client.

private FakeServer _fakeServer;
[SetUp]
public void SetUp()
{
   _fakeServer = new FakeServer(8081);
}
[Test]
public void OrderService_WhenFetchingAValidOrder_CustomerDetailsAreReturned()
{
   _fakeServer.Expect.Get("customer/1").Returns(new { Id = 1, Name = "Joe Bloggs"});
   _fakeServer.Start();
   orderService = new OrderService(new CustomerApiClient("http://localhost:8081")); //concrete class with endpoint injected
   Order order = orderService.GetOrder(123);
   Assert.That(order.Customer.Id.Equals(1));
}

Tests like this are still expensive to write, however we do get 2 big benefits

  1. our tests covers the whole of the API client, including any logic and dependencies within the client itself
  2. we can easily adapt the test to run against the actual API, by injecting a real URL into the client, and thus reuse it as an integration test

Teams at JUST EAT don’t write all of their tests using JustFakeIt, that’d be overkill! Most teams use JustFakeIt when writing bigger outside-in tests, or when writing SDKs for their own internal APIs, and that’s where we’ve JustFakeIt to have the biggest benefit.

Downloads

Source code is available on GitHub here: https://github.com/justeat/JustFakeIt NuGet packages available here: https://www.nuget.org/packages/justfakeit

Your email address will not be published. Required fields are marked *