Unit testing your Azure functions - part 2: Queues and Blobs
In our last installment, we discovered how to start unit testing our Azure Functions, looking at the HTTP Trigger (and evaulating the function's HTTP response). This time, we'll take a look at using the QueueTrigger, getting supporting data from table storage and outputting Blobs. Yup, I'm tossing you into the deep end!
In the function above, we are introduced to three types that we need to deal with in our unit tests - the IQueryable interface, IAsyncCollector and the CloudBlockBlob concrete type. IQueryable is easy enough to work with:
Another super-simple implementation. With those two out of the way, let's tackle CloudBlockBlob:
Alright! We are now ready to write our unit tests:
With these tools in our toolbelt, we can now successfully handle the most common function scenarios. Best of luck!
What are we trying to accomplish?
This time, we will test a function that performs some arbitrary business logic on bank accounts. The function responds to a storage queue request, logs each command and ensures that each received command is executed exactly once. Almost like a real-world function, huh? ;-)In the function above, we are introduced to three types that we need to deal with in our unit tests - the IQueryable interface, IAsyncCollector and the CloudBlockBlob concrete type. IQueryable is easy enough to work with:
Introducing the Testable Async Collector
When I first started exploring this topic, I figured I could just use my favourite mock framework - MOQ - to mock the IAsyncCollector interface. However, after writing the first unit test, I balked at the amount of ugly code was required - and would be required again and again - if I continued down that path. So instead, I wrote a new type:Another super-simple implementation. With those two out of the way, let's tackle CloudBlockBlob:
Alright! We are now ready to write our unit tests:
With these tools in our toolbelt, we can now successfully handle the most common function scenarios. Best of luck!
Comments