C# – How to unit test code that uses HttpClient

HttpClient is a dependency. Like any other dependency, you need to pass it into your code (aka dependency injection). By passing it in, you can mock it out in unit tests. There are two approaches to mocking it out: Wrap the HttpClient and mock out the wrapper. Use a real HttpClient with a mocked out … Read more C# – How to unit test code that uses HttpClient

C# – Parameterized tests with MSTest v2

Here’s an example of how to parameterize your tests using the built-in MSTest v2 test framework: There are 3 steps: Add parameters to your test method. Use [DataTestMethod] instead of [TestMethod]. For each test case, add [DataRow(…)] to pass in the parameters for that test case. What parameters can you pass in? You pass in … Read more C# – Parameterized tests with MSTest v2

C# – How to unit test async methods

Let’s say you have the following async method you want to test: Here’s how to unit test this: This is awaiting the method you’re testing. To await it, you must make the unit test method return async Task. This example is a bit simplistic. In the real world when you are working with async methods, … Read more C# – How to unit test async methods

Refactoring the Switch Statement code smell

The Switch Statement code smell refers to using switch statements with a type code to get different behavior or data instead of using subclasses and polymorphism. In general, it looks like this: This switch(typeCode) structure is typically spread throughout many methods. This makes the code difficult to extend, and violates the Open-Closed Principle. This principle … Read more Refactoring the Switch Statement code smell

Refactoring the Primitive Obsession code smell

The Primitive Obsession code smell refers to code that is using primitive types (ex: int, bool) instead of classes and enums. This defeats one of the benefits of object-oriented programming: encapsulation. Code Smell: Primitive Obsession. Definition: Using primitives instead of encapsulating them with a class. Solution: Encapsulate the primitive fields: Move them to a new … Read more Refactoring the Primitive Obsession code smell

Refactoring the Large Class code smell

The Large Class code smells refers to a class that has too many responsibilities. It’s doing too much. Ideally a class should only have one responsibility (Single Responsibility Principle). Code Smell: Large Class Definition: A class has too many responsibilities. Solution: Identify all of the distinct responsibilities of the Large Class. For each responsibility, extract … Read more Refactoring the Large Class code smell

How to refactor code that has no tests using the Golden Master technique

The number one rule when refactoring is to always have tests in place before you start refactoring. Without tests, refactoring becomes a risky endeavor. Code that doesn’t have tests is often not designed to be testable. In order to make the code testable, you have to refactor it. See how this seems like an impossible … Read more How to refactor code that has no tests using the Golden Master technique

Refactoring the Long Method code smell

In this article I’ll be walking through an example of how to refactor the Long Method code smell. Code Smell: Long Method Definition: A method has too many lines of code, making it hard to understand. Solution: Extract duplicate code into a new method Extract code into smaller methods Long Method code smell example Here’s … Read more Refactoring the Long Method code smell

How to mock static methods

The need to mock static methods in order to add a unit test is a very common problem. It’s often the case that these static methods are in third-party libraries. There are many utility libraries that are completely made up of static methods. While this makes them very easy to use, it makes them really … Read more How to mock static methods

The bottom-up principles of unit testing, refactoring, and pragmatism

After years of coding and reading dev books I’ve distilled my coding principles down into these three simple principles: unit testing refactoring pragmatism I write these down in my notebook and repeat them to myself on a daily basis. These are my coding principles. It’s important to know your principles. Your principles act as a … Read more The bottom-up principles of unit testing, refactoring, and pragmatism