Unit testing with angularjs, requirejs, jquery, karma and jasmine.
19 July 2015
Unit testing with angular requirejs karma and jasmine
In this example we're going to create a sample test for the an application called "FarmApp", which should ressemble an imaginary farm.
Here we are testing our AnimalListCtrl.js a simple angular JS controller that lists animals of our current farm app.
project directory tree
services.js
Our services that will interact with a backend resource:
Controllers
each controller, is layered in such a way that their corresponding test and html view are bundled together in the same folder:
animalList.js
Here is the code for the animal list controller, which will be responsible for fetching a list of animals from the service.
karma.conf.js
In order to trigger unit tests in our application we will use Karma along with Jasmine for JS Unit tests.
test-main.js
test-main is where we define all requireJS config setup for tests.
requirejs.config({// Karma serves files from '/base'baseUrl:'/base/app/js',paths:{jquery:'../lib/bower_components/jquery/dist/jquery.min',angular:'../lib/bower_components/angular/angular.min',angularAnimate:'../lib/bower_components/angular-animate/angular-animate.min',angularCookies:'../lib/bower_components/angular-cookies/angular-cookies.min',angularResource:'../lib/bower_components/angular-resource/angular-resource.min',angularRoute:'../lib/bower_components/angular-route/angular-route.min',angularScenario:'../lib/bower_components/angular-scenario/angular-scenario',angularMocks:'../lib/bower_components/angular-mocks/angular-mocks',angularTouch:'../lib/bower_components/angular-touch/angular-touch', angularSanitize:'../lib/bower_components/angular-sanitize/angular-sanitize.min',jqueryui:'../lib/bower_components/jquery-ui/jquery-ui.min',bootstrap:'../lib/bower_components/bootstrap/dist/js/bootstrap.min',angularBootstrap:'../lib/bower_components/angular-bootstrap/ui-bootstrap-tpls'}, shim:{'angular':{'deps':['jquery'],'exports':'angular'},'angularResource':{'deps':['angular'],exports:'angularResource'},'angularRoute':{'deps':['angular']},'angularAnimate':{'deps':['angular']},'angularCookies':{'deps':['angular']}, 'angularMocks':{deps:['angular']},'angularTouch':{deps:['angular']},'angularScenario':{deps:['angular']},'angularSanitize':{deps:['angular']},'angularBootstrap':{deps:['angular','bootstrap']},'bootstrap':{deps:['jquery']}}, priority:['angular']// ask Require.js to load these files (all our tests)deps:tests,// start test run, once Require.js is donecallback:window.karma.start});
animalListSpec.js
In the unit test, in this case animalListSpec.js, are all the tests that should be used by Karma for testing the animalList controller and view:
Notice how mockAnimalService mocks calls to animalService, returning a fake animal list based on JSON test data.
Also notice how here spyOn is used to actually monitor these calls and and.callThrough() gurantees the calls will be passed through,
otherwise the calls to the mock functions wouldn't be done.
Once Unit test is created you can run tests by either building and testing with grunt, in the command line in folder where Gruntfile.js is located,
or either by typing karma start where karma.conf.js file is located.