Unit Testing React Contexts

When unit testing a React application, you'll want to test every aspect of it. This will include testing each of your contexts.

Keep in mind that creating a context often involves subscribing to something (like a service) and, it is being consumed (such as by components). Understanding that a context is similar to an environment in which a group of components exists is key. Therefore, our tests will need to mimic both behaviors—stubbing a service and consuming our context. Below, I'll demonstrate just how to do that.

Read more


Using Sinon+Rewire for Unit Testing with Private Methods

How many times have you tried to follow good programming practices by creating well-defined, single-purposed methods and only exposing the necessary public methods outside of your library and become frustrated with attempting to write unit tests for your code? You then begin to question your decisions about which method(s) should be `public` and which methods should be `private`. Additionally, you start to wonder if 100% code coverage is truly necessary. I mean, 75% of code coverage isn't really that bad, is it? It's better than 10%, right?

If you scour the web for suggestions, there's a number of less-than-optimal solutions for this problem. First, I'd like to cover some of these solutions and why they are less than ideal. Second, I'll provide some production code that contains a private method along with how to successfully achieve 100% coverage with unit tests. The examples provided are in Node.js.

Read more


Karma + Jasmine + Visual Studio (including Visual Studio Code)

I've been developing a LOT of Angular applications lately. Some of them are hybrid projects (Angular w/ MVC); some of my projects have been completely separated (Angular for client-side, with a separate project for an API). Regardless, I always want to ensure that my code has been thoroughly tested with unit tests and acceptance (E2E) tests. When developing hybrid projects, my preferred IDE is Visual Studio. When developing a pure, client-side project, my preferred IDE is Visual Studio Code as it has a lot less remnants/artifacts tied to a solution (.vs, .xproj, .csproj, etc.) eliminating the need for a ridiculously large .gitignore file.  Additionally, I will use WebStorm depending on the need.

Jasmine is a great framework for providing both unit testing and end-to-end, acceptance testing.  Coupled with Karma, Jasmine can monitor file changes to our client-side code and execute tests on every file change in order to ensure that all tests are always passing.  In this blog post, I will demonstrate how to set up and use Karma and Jasmine in both development environments.

Read more


Two Ways to Mock System.Web.HttpContext

The other day I was writing some unit tests for testing my MVC application's forms authentication classes.  I needed to Mock the System.Web.HttpContext object.  There are a couple of ways to do this depending on the version of Visual Studio you are using (i.e. Professional, Premium/Ultimate/Enterprise) and how deep you wish to provide some default data.   One takes a little more leg work and requires some manual data setting, but gives you greater control.  While the other requires less coding for simple basic use.

Read more


Fakes Issue with System.Web

The other day I was writing some unit tests on an MVC project.  I needed to mock (fake) an HttpRequest object using the System.Web assembly.  However, there was a strange issue in creating the fakes assembly and adding it to my Visual Studio solution.  Namely, it didn't show up.Read more


Unit Testing an MVC Model

Do your MVC model properties contain attributes?  Have you ever wanted to unit test the properties to verify that the ModelState fails or succeeds based on given values?  Below is a static method that can be used for your unit tests as you test your models.
Read more


Why Unit Test?

As a Microsoft Certified Trainer over the past 2 years, I've had the opportunity of teaching hundreds of professionals how to properly engage in unit testing.  Amazingly enough, even though unit testing has been around quite a while, it is still a concept that is very infantile in the corporate arena.  Most developers who have adopted unit testing are those who work for start-ups or the few companies who've started to embrace agile methodologies executing test-driven development (TDD).

Read more