C# – Access modifiers

When you’re using a Queue class, you only need access to the Enqueue() and Dequeue() methods. Everything else is private. You don’t need to know how data is stored or managed internally. That’s encapsulation, and it’s enabled by access modifiers. Access modifiers are used to hide members from other code. In C#, there are four … Read more

Algorithm Explained: Sum two big integers the hard way

Problem statement: Sum two big integers that are passed in as strings. Return the sum as a string. In other words, implement the following method: Constraint: Don’t use the built-in BigInteger class (note: this is the name in C# and may have a different name in other languages). Do it the hard way instead. If … Read more

ASP.NET Core – How to unit test an ApiController

The key to unit testing an ApiController class is to mock out all of its dependencies, including the controller’s HttpContext property, like this: If the controller method you’re testing uses anything from the HttpContext, then you’ll want to swap in your own value. Otherwise HttpContext will be null and you’ll get a NullReferenceException. Fortunately Microsoft … Read more

C# – Join strings with a separator, ignoring nulls and empty strings

Normally when you want to join strings using a separator, you’d use string.Join(). However, the problem with string.Join() is it doesn’t ignore nulls or empty strings. Take a look at the following examples: If you want to filter out nulls and empty strings, you can filter the list of strings yourself and pass it into … Read more

C# – Convert a list of strings into a set of enums

Let’s say you have a list of HTTP status codes that you read in when the service starts up (perhaps from appsettings.json or from the database). Whenever you send an HTTP request, you want to check if the returned status code is in this list of status code. To make things more efficient, you want … Read more

C# – Unit test doesn’t finish and stops all other tests from running

Problem You have a unit test that doesn’t finish, and it prevents other tests from running. There’s no indication that the test passed or failed. It just stops running. When you run all of the tests together, some tests might finish, but once this one bad test stops, it prevents other tests from running. This … Read more

C# – Test fails with message ClassInitialize has the wrong signature

When you are using the ClassInitialize / ClassCleanup attributes, your tests may fail with the following error message: Method TestClassInit has wrong signature. The method must be static, public, does not return a value and should take a single parameter of type TestContext. Additionally, if you are using async-await in method then return-type must be … Read more

C# – How to unit test code that uses Dapper

Dapper makes your code difficult to unit test. The problem is that Dapper uses static extension methods, and static methods are difficult to mock out. One approach is to wrap the Dapper static methods in a class, extract out an interface for that wrapper class, and then dependency inject the wrapper interface. In the unit … Read more

C# – Use StringAssert when testing a string for substrings

When you’re testing if two strings are equal, you can simply use Assert.AreEqual(). When you’re testing if a string contains a substring or a pattern, typically developers use Assert.IsTrue() with a substring method or regex. You should use StringAssert instead, because it gives better failure messages. The key problem is Assert.IsTrue() gives useless information. You … Read more

Event-driven .NET: How to unit test code that depends on an event

Testing code with a dependency on an event can be tricky. You may be tempted to directly unit test the event handling method. However, that doesn’t prove that code under test is listening for the event. It’s better to really fire the event, and then test side effects of the event handling method. Firing the … Read more