A quick trip around javascript and Angular unit testing using Jasmine and Chutzpah.
Testing the webwith Chutzpahand Jasmine
View Slide
Page“I’m a c# guy that uses NUnit”› In the javascriptworld, Jasmine is yourNUnitequivalent.(http://jasmine.github.io/)› In Jasmine, you create ‘Suites’ thatdescribe a set of expectations youhave for the thing you want to test.› Jasmine also allows you to also setyour expectations within these suites./ Copyright ©2014 by Readify Pty Ltd2
PageSo how do I do X in Jasmine?› Setup and teardown:› Ignore tests:/ Copyright ©2014 by Readify Pty Ltd3
PageCool, now how do I run my tests?› Chutzpah! (https://github.com/mmanela/chutzpah)› Chutzpah generates html to run our specs withJasmine in a headless browser (phantomjs).› It will also generate html to run our specs in ourbrowser of choice (handy for debugging tests!)› It also takes care of loading dependencies for us whereneeded./ Copyright ©2014 by Readify Pty Ltd4
PageSo how to we hook it all up?› Install-Package Chutzpah› Install the Chutzpah VS Extensions› Add and configure your chutzpah.json› Don’t worry about installing Jasmine! Chutzpahbundles it up for you › Write your hello world unit test!/ Copyright ©2014 by Readify Pty Ltd5
PageMan this thing is busted!› Chutzpah is a bit of a black box when used from VS – ifit doesn’t seem to be working from VS, you can alwaysuse the command line runner.› Running chutzpah.console.exe {fullpathtofile.js} /tracewill give you a detailed log of the test run namedchutzpah.log alongside the console exe (in yourpackages folder)/ Copyright ©2014 by Readify Pty Ltd6
PageOkay cool, now how about Ng?› To test an Ng app, because of how angular’smodule / DIsystem works, we need to make sure we include allnecessary references to the applications scripts in ourchutzpah.json› If you have a bunch of bootstrapping activity in yourstandard app.js, you may want to craft a slimmed downversion for testing/ Copyright ©2014 by Readify Pty Ltd7
PageDude, where’s my Mocks?› ngMock(https://docs.angularjs.org/api/ngMock)› Injects and mocks most of the core angular services for us,such as $http and $log› It *must* be loaded by Chutzpah after angular.js (it overridesdefault behaviour)› No further wireuprequired!› Jasmine also provides some mocking functions similar toNSubstitute/ Copyright ©2014 by Readify Pty Ltd8
PageExample Ng Test/ Copyright ©2014 by Readify Pty Ltd9describe("Landing Page Directive", function() {var $sut;describe('If form is started', function() {var goToDashboardCalled = false;beforeEach(function() {module(function($provide) {$provide.service('utilityService', function() {return {goToDashboard: function() {goToDashboardCalled = true;}}});});inject(function($compile, $rootScope, $httpBackend) {$httpBackend.expectPOST('apiBaseUrlforms/getbasicformdetails').respond(200);$rootScope.formStarted = true;var elem = angular.element("");$sut = $compile(elem)($rootScope);$rootScope.$digest();});});it('Redirects to the dashboard.', function() {expect(goToDashboardCalled).toBe(true);});});});
PageHey what were those things?› Module: allows us to replace services to be injected intoour class under test. This allows us to mockother thingsout.› Inject: allows us to mutate mocked dependencies to setthem up for our SUT, and also supply them when buildingour SUT.› These are both given to us via ngMock, and for ease of useare attached to the window object for us./ Copyright ©2014 by Readify Pty Ltd10
PageNg testing gotchas› Http interactions *must* be mocked.› You can only set up dependencies via Module() beforeyour call to Inject(). If you call it after… BOOM.› If something doesn’t seem to be working, rememberto double check your chutzpah.jsonand the htmlgenerated by Chutzpah for script errors!/ Copyright ©2014 by Readify Pty Ltd11
PageStaying DRY› So you have a bunch of code you want to run beforeevery test? Sick of copy pasta?› Jasmine allows us to define a spec-helper.js file whichcan include global beforeEvery and afterEveryfunctions!› This can be a useful place to bootstrap things like Ngconstants, if you have some./ Copyright ©2014 by Readify Pty Ltd12
PageBonus slide: CI› So now I have my tests, how do I plug them into my CIpipeline?› Use the Chutzpah console exe that you checked in withyour chutzpah package because you are a gooddeveloper and detest package restore.› And it goes a little something like this…/ Copyright ©2014 by Readify Pty Ltd13