Just Simply Code

Another developer blog

  • Home
  • About
  • Register
  • Policy

Unit Test

Integration tests with dockerized SQL Server

23 October 2022 / Leave a Comment

Recently, a friend of mine asked me if there’s a better way to run integration tests without using in-memory DB because in-memory DB does not behave exactly like the real thing. In my opinion, the repository should be simple and not contain any business logic. Hence mocking the data in the in-memory DB for the … [Read more…]

Posted in: Uncategorised Tagged: .Net Core, Docker, EF Core, Integration Test, SQL, Unit Test

JSON serialize mock object in unit test

6 October 2019 / Leave a Comment

I had to unit test a method that calls a service which returns an interface type. This returned interface is then serialised into JSON string and stored into to some database. This is an example of such method This is the example of the unit test I thought serialising any mock object to JSON should … [Read more…]

Posted in: Uncategorised Tagged: Json Serialisation, Unit Test

Comparing Json strings in unit tests

14 September 2019 / Leave a Comment

There are a couple ways to verify if 2 Json strings are equivalent. Using DeepEquals on JObject There’s a static function DeepEquals on JObject which compares the values of 2 tokens and all their descendant tokens. We can parse the Json strings into JObject and then use this function to compare them. Using FluentAssertions Another … [Read more…]

Posted in: Uncategorised Tagged: Json, Unit Test

Ignore specified members field when serialise to Json string

2 February 2019 / Leave a Comment

I have a generated WCF client with class similar to the sample below In my unit test, I instantiate an instance of SampleClass with a value of Cost field but without setting the CostFieldSpecified to true. When I serialise it to Json string, the Cost field’s value is 0 instead of the value I set … [Read more…]

Posted in: Uncategorised Tagged: Json, Json Serialisation, Unit Test

Testing Context

3 November 2018 / Leave a Comment

This post I will show how to create a testing context which can be used in unit tests to automatically construct the testing target. My solution is based on this article from https://dotnetcoretutorials.com. I’ve customised it a bit to allow mock Target and automatically inject all the mocks during setup. The customisation will give you a mock … [Read more…]

Posted in: Uncategorised Tagged: Dependency Injection, NUnit, Unit Test

Mocking DelegatingHandler SendAsync method in unit test

6 October 2018 / Leave a Comment

Previously I showed how to mock SendAsync method in HttpClient so it can be unit tested. In this post, I will show how to mock the SendAsync method in DelegatingHandler so it can be unit tested as it allows you to mock the response and your test does not need to make a real Http request. The first problem … [Read more…]

Posted in: Uncategorised Tagged: DelegatingHandler, Mock data, Unit Test

Mocking HttpClient SendAsync method

22 September 2018 / Leave a Comment

Previously I showed how to log all HttpClient outgoing requests. I later added some logics for adding default headers in the SendAsync method in OutgoingHttpClient. I wanted to unit test this to make sure my logic is correct. I thought I could mock the HttpClient the normal way but since the SendAsync method in HttpClient is not virtual, I get … [Read more…]

Posted in: Uncategorised Tagged: HttpClient, Mock data, Unit Test

Builder patterns in unit tests

11 August 2018 / Leave a Comment

I like using the Builder pattern for building test data in unit tests. Builder pattern makes it clean if I need to build a valid object, in the unit tests, with the minimum required properties. It also makes it clear when I need to build an object and explicitly specify a different value for any of … [Read more…]

Posted in: Uncategorised Tagged: Builder Pattern, Unit Test

Make method or class that uses current DateTime unit testable

14 July 2018 / Leave a Comment

So often we have to use DateTime.UtcNow or DateTime.Now in our code. To make it easier to mock current date time in unit test and to make unit tests more resilient, it’s better to not use DateTime.UtcNow or DateTime.Now directly but rather pass it in or make it as a property. Let’s say we have a … [Read more…]

Posted in: Uncategorised Tagged: DateTime, Unit Test

Configure AutoMapper in unit tests

30 June 2018 / Leave a Comment

It’s very common to use AutoMapper to map one object to another. However if not configured, unit testing functions that use AutoMapper will throw a missing mapping exception. I’ve seen in previous projects 2 different approaches developers use AutoMapper. I’m going to show how to configure AutoMapper in unit test for each of the approaches. … [Read more…]

Posted in: Uncategorised Tagged: AutoMapper, NUnit, Unit Test
1 2 Next »

Search Posts

Recent Posts

  • Override IHttpRequestBodyDetectionFeature.CanHaveBodyOverride value in .NET Core Web API
  • Allow empty request body in PUT and POST HTTP requests in .NET Core Web API
  • Modify request body with middleware in .NET Core Web API
  • Running MS Orleans on AWS ECS
  • Respect leading and trailing spaces when querying SQL Server
Support my blog here!Support my blog!

Archives

  • July 2024
  • June 2024
  • July 2023
  • April 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • July 2019
  • June 2019
  • May 2019
  • April 2019
  • March 2019
  • February 2019
  • January 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • June 2018
  • May 2018
  • October 2017
  • September 2017
  • July 2017
  • June 2017
  • May 2017
  • April 2017
  • March 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
  • October 2016
  • September 2016
  • July 2016
  • June 2016
  • March 2016
  • February 2016
  • January 2016

Copyright © 2025 Just Simply Code.

Me WordPress Theme by themehall.com