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

2015/10/021 min read
bookmark this
Responsive image

Table of Contents

  1. Install packages
  2. Create AngularJS app module
  3. Create AngularJS controller
  4. Write Javascript Unittest
  5. Get Chutzpah Test Adapter

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.

///
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.