Javascript Unit Test With Jasmine, Angular and Chutzpah with Visual Studio

2015/10/21 min read
bookmark this
Responsive image

The following blog shows how to run the AngularJS unit test just like how you run C# unit test, so basically, you'll run your AngularJS at the visual studio test explorer window.

Install packages

You'll need to install the following two packages, AngularJS.Core and JasmineTest.

install-package AngularJS.Core
install-package JasmineTest

Create AngularJS app module


var app = angular.module('app', []);

Create AngularJS controller

Just create a simple controller with score.test is test.


app.controller('myController', function ($scope) {
    $scope.test = 'test';
});

Write Javascript Unittest

Following this, we'll create a Javascript unit test code by Jasmine.

/// <reference path="{{path to the javascript}}"/>
describe('testAngular ', function () {
    //initialize Angular
    beforeEach(module('app'));
    var scope;
    beforeEach(inject(function ($controller, $rootScope) {
        scope = $rootScope.$new();
        var ctrl = $controller('myController', { $scope: scope });
    }));

    it('test scope is test', function () {
        expect(scope.test).toBe('test');
    });
});

Get Chutzpah Test Adapter

Chutzpah Test Adapter for the Test Explorer

Once you install Chutzpah you can just run the unit tests like C# unit tests.