C# – How to call a static method using reflection

Here’s how to call a static method using reflection: Note: This static method is parameterless. If you have parameters, you have to pass them in like this .Invoke(null, param1, param2). Example – passing static method names to a parameterized unit test With parameterized tests you can only pass in compile-time constants. This means you can’t … Read more C# – How to call a static method using reflection

TDD by Example: How to add tests against code that doesn’t exist yet

I personally use Test-Driven Development (TDD) on a daily basis because it allows me to focus on solving one test case at a time in the simplest way possible, and then refactor with the safety net of unit tests. It’s a bottom-up approach, and is a natural fit for my coding principles (unit test, refactor, … Read more TDD by Example: How to add tests against code that doesn’t exist yet

C# – Hex string to byte array

This article shows code for converting a hex string to a byte array, unit tests, and a speed comparison. First, this diagram shows the algorithm for converting a hex string to a byte array. To convert a hex string to a byte array, you need to loop through the hex string and convert two characters … Read more C# – Hex string to byte array

C# – Use Assert.ThrowsException instead of ExpectedException attribute

If you’re using the built-in MSTest unit testing framework, you may be used to the old way of asserting exceptions by using the [ExpectedException] attribute. The following example illustrates one of the key problems with this approach: The ArgumentNullException is being thrown, so the test passes. However, the exception is being thrown in the arrange … Read more C# – Use Assert.ThrowsException instead of ExpectedException attribute

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# – Can’t pass decimal parameter in DataTestMethod

I have a parameterized unit test with decimal parameters. When I run the test, I get the following exception: System.ArgumentException: Object of type ‘System.Double’ cannot be converted to type ‘System.Decimal’. Solution Change the parameters to doubles and convert them to decimals inside the test method. Why is it throwing an exception? You have to pass … Read more C# – Can’t pass decimal parameter in DataTestMethod

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 test that your code can handle another culture’s date format

Let’s say you’re using DateTime.Parse() to convert a date string into a DateTime object. By default, DateTime.Parse() uses CultureInfo.CurrentCulture to figure out the date format. The current culture ultimately comes from your OS settings. So when you run this code on a computer that is using the en-US locale, the current culture will automatically default … Read more C# – How to test that your code can handle another culture’s date format

Moq – How to return different values each time a mocked method is called

When you’re mocking a method that’s called multiple times, you may want to change the behavior of the method each time it’s called. The way you do this with Moq is by using SetupSequence(), like this: Note: You can also make it throw an exception in the sequence. Example of code I want to test … Read more Moq – How to return different values each time a mocked method is called

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