BKM BKM - 1 month ago 4
AngularJS Question

Writing Unit Test Case in Angular JS using Jasmine

I am building an application using Angular JS. As I am new to it I don't know much about writing test cases in it.

Suppose I have a service:


factory('MainPage', function($resource,BASE_URL){

return $resource("my api call", {}, {query: {method:'GET'}, isArray:true});


My Controller:

var app = angular.module('MyApp')

$scope.mainpage = MainPage.query();

How I write test case for this controller in Angular JS using Jasmine.


You would write something along these lines:

describe('MyApp controllers', function() {

  describe('MainCtrl', function(){

    it('should populate the query', function() {
      var scope = {},
          ctrl = new MainCtrl(scope);


This is well documented, see the AngularJS tutorial for a quick reference, it's also suggested to read the Jasmine docs (!).

You'd also want to spy on the query() method, see here on how.