Just Simply Code

Another developer blog

  • Home
  • About
  • Register
  • Policy

Unit Test

Mocking Entity Framework Core DBContext for unit testing

2 June 2018 / Leave a Comment

In my new project, we use EF Core for ORM. We have unit tests for the repository layer where the DbContext is used. I’m going to show how to use in-memory database to create the DbContext. I’ll use the model below for example public class Article { [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int ArticleNumber { get; set; } … [Read more…]

Posted in: Entity Framework, NUnit, Unit Test Tagged: Entity Framework, NUnit, Unit Test

Excluding certain properties when asserting objects with FluentAssertion

21 July 2017 / Leave a Comment

It’s very common that we assert if two objects are equivalent in unit tests. Using FluentAssertion, this looks like the following actual.ShouldBeEquivalentTo(expected); However, there are times where you want to exclude certain properties when comparing objects. This might be because the value of the property cannot be predicted or you just don’t want to include … [Read more…]

Posted in: Unit Test Tagged: Unit Test

Let’s just not unit test our code

20 May 2017 / Leave a Comment

This post is part of a series of blog posts about bad codes or bad practices I’ve come across. Note that this is my personal opinion and some of the things I mention could be debatable. Post 1: Code review, why it matters Post 2: Not including JS, CSS and static content files in project Post … [Read more…]

Posted in: Bad Practice Series, Unit Test Tagged: Bad Practice Series, Unit Test

Validator not validating range attribute on properties

9 April 2017 / 1 Comment

I have a submit model with a property of type decimal that represents a percentage. I use the range attribute to only allow values from 0 to 100. I have a unit test where I use the Validator to test this validation on the submit model. However, when I pass in an input that’s outside the … [Read more…]

Posted in: Unit Test, Validator Tagged: Unit Test, Validator

Using TestCaseData from NUnit for better test name

12 March 2017 / Leave a Comment

I tend to name my unit tests with the following convention: NameOfMethod_Inputs_Should_ExpectedResult For example, I have a method called Multiply which multiplies two integer parameters. If I have a test case to test this method with 2 integers 3 and 4, the test would be called Multiply_3_And_4_Should_Be_12 This has been working well for me until I recently come … [Read more…]

Posted in: NUnit, Unit Test Tagged: NUnit, Testing, Unit Testing

Organising unit tests into partial classes

19 February 2017 / Leave a Comment

When unit testing a service, I tend to have a base class where I do all the setting up/initialisation. For each method in the service, I’d have a separate unit test class which derives from the base class. For example, if I want to unit test two methods GetAllNews and GetNewsById in SampleNewsService, I’ll have a SampleNewsServiceTestsBase … [Read more…]

Posted in: Unit Test Tagged: Unit Test

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