DEV Community

Sam Ferree
Sam Ferree

Posted on

1 2

How to avoid mocking repository for Unit Tests

I commonly see code written like this, even in courses.

public class DeskBookingProcessor
{
  public readonly IBookingRepository _bookingRepository;
  public readonly int _capacity;

  public async Task<Booking?> Book(BookingRequest request)
  {
    var currentBookings = await _bookRepository.GetBookingsAsync(request.Date);
    if (currentBookings + 1 > _capacity)
    {
      return null;
    }
    else
    {
      var booking = new Booking(request);
      await _bookingRepository.SaveAsync(booking);
      return booking;
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

The drawback is you have to mock a whole IBookingRepository (Even the methods this feature doesn't use) to test your booking logic.

Alternatively, you could do this

public class BookingAgent
{
  public Booking? Book(BookingRequest request, int currentBookings, int capacity) =>
    currentBookings + 1 > capacity
      ? null
      : new Booking(request);
}
public class DeskBookingProcessor
{
  public readonly IBookingRepository _bookingRepository;
  public readonly int _capacity;

  public async Task<Booking?> Book(BookingRequest request)
  {
    // Impure Collect
    var currentBookings = await _bookRepository.GetBookingsAsync(request.Date);
    var agent = new BookingAgent();

    // Pure Calculation
    var booking = agent.Book(request, currentBookings.Count(), capacity);

    // Impure Commit
    if (booking != null) await _bookRepository.SaveBookingAsync(booking);

    return booking;
  }
}
Enter fullscreen mode Exit fullscreen mode

Now your unit tests don't need to mock impure dependencies, just test the BookingAgent by passing the parameters.

Leave it to your integration tests flex the combination of the pure logic and impure dependencies. Prefer the kind that stand up a whole test server and send HTTP requests and check HTTP responses.

That is all.

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay