Better assertions when verifying Moq’d methods have been called

Man wearing fake glasses and moustache

Moq, according to its GitHub page, is “The most popular and friendly mocking framework for .NET”. Mocking frameworks allow you to create a ‘fake’ version of a dependency (typically an interface, or abstract class). With this fake, you can then instruct the methods return values, or invoke behaviour. Alternatively you can assert the methods were called, with a certain set...

Azure function not receiving json messages using Topic Service Bus Trigger


While developing an Azure Function application, using this tutorial, I encountered a problem. Ultimately, using func new generated my function (the run.csx file) which looked like this: C# public static void Run(string mySbMsg, TraceWriter log) { log.Info($"C# ServiceBus topic trigger function processed message: {mySbMsg}"); } 1234 public static void Run(string mySbMsg, TraceWriter...

Out Of Memory exception when deserializing with JSON.net – Use Streams instead


Calling an api and deserializing the returned json into a type is something I have to do quite often. I used to use the following: C# //assume client is an instance of HttpClient - this part isn't important var response = await client.PostAsync(url, content); //read the response as a string var responseString = await response.Content.ReadAsStringAsync(); //use json.net to deserialize this into a...

Mocking calls to ApplicationContext.Current.Services


My current contract involves working on a project based on Umbraco. Unit testing Umbraco, can be a bit tricky, especially given the existence of the static ApplicationContext.Current.Services class, which contains handy references to the Umbraco services – provided by a static type! We can’t mock this. So, I created a little wrapper around this that would allow us to mock the returned service...

Entity Framework migrations – names are case sensitive


In a rush, I’d called my migration “vendorServiceKEy” Which had resulted in my migration being named 201512102031458_vendorServiceKEy I then ran migrations with update-database. Now, when I renamed the class manually, to sort out my OCD, the next time my application ran, I got There is already an object named xxx in the database My _MigrationHistory table contained the following...