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


AjaxOnly Attribute for MVC Actions

Ever wanted to restrict actions to only responding to Ajax requests?  How about restricting them through the use of a custom attribute?

Read more


Automation Testing with SeleniumHQ

In an earlier post, I provided step-by-step instructions in how to perform Behavior Driven Development using Visual Studio, SpecFlow, WatiN and DryRunner.  However, I've had a lot of students and blog readers ask me about Selenium, a more-common browser automation tool.  So, I'm writing this post to show how to accomplish BDD and automated test driven development (ATDD) using Selenium.Read more


Behavior Driven Development with SpecFlow + WatiN + DryRunner

In the previous post, we examined some of the principles behind BDD.  If you read it, I'm sure you're thinking, "Wow! That's great! But, how do I accomplish this in Visual Studio?"  There are a myriad of posts on the Internet that demonstrate different components.  However, there's not really a single post with all of the information compiled.  For that reason, I'm going to provide a step-by-step tutorial on how to perform BDD with Visual Studio. Additionally, I will show you how to perform automated testing using your Gherkin scripts.

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


Counting the Factors of a Number

By definition, a factor is one of two or more positive numbers that when multiplied together produce a given product.  For example, given the integer 24, the count of factors 8.

The below algorithm tests the range of numbers from 1 to a given integer.  If the given interger divided by the current number returns a remainder of 0, then the current number is a factor of the given integer.

 

public int GetFactorsCount(int i)
{
    return Enumerable.Range(1, i).Count(x => i % x == 0);
}

 

Get the source control, including unit tests: GetFactorsCount.zip (9.43 kb)


Getting All Factors for a Number

By definition, a factor is one of two or more positive numbers that when multiplied together produce a given product.  For example, given the integer 24, the factors are [1, 2, 3, 4, 6, 8, 12, 24].

The below algorithm tests the range of numbers from 1 to a given integer.  If the given integer divided by the current number returns a remainder of 0, then the current number is a factor of the given integer.

public List<int> GetFactors(int i)
{
    return Enumerable.Range(1, i).Where(x => i % x == 0).ToList();
}

 

Get the source control, including unit tests: GetFactors.zip (9.85 kb)