Writing basic units for Azure Functions triggered by the Change Feed is straightforward with xUnit

While refactoring some of our microservices at work, I came across a service that didn’t have any unit tests for them! This service uses the Azure Cosmos DB Change Feed to listen to one of our write-optimized containers related to customers. If a new customer is created in that container, we then pick up that Customer document and insert it into a read-optimized container (acting as an aggregate store) which has a read friendly partition key value.

This read-optimized container is then utilized by other services within our pipeline when we need to query the aggregate for our Customer data.

Most of our services are triggered by message brokers (Event Hubs, Service Bus, etc.) so our process for unit testing these services is pretty standardized. But for whatever reason, this service didn’t have any unit tests, which is pretty bad in my opinion. So I’d thought I’d have a crack at it.

Turns out, it’s fairly straightforward.

So in this article, I’m going to show you how straightforward it is to write basic unit tests for an Azure Function that is triggered by the Azure Cosmos DB Change Feed using xUnit. This is the unit testing framework we use at work and I also use it in my side projects as well since it’s free, open-source, and super easy to get your head around.

Wait, What is the Change Feed again?

The Azure Cosmos DB Change Feed is a persistent record of changes that take place in a container in the order that they occur. It listens to any changes in a container and then outputs a sorted list of documents that were changed in the order in which they were modified.

#c# #xunit #azure

Unit Testing The Azure Cosmos DB Change Feed In xUnit And C#
2.55 GEEK