BKM
BKM

Reputation: 7079

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:

angular.module('MyApp').

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

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

}).

My Controller:

var app = angular.module('MyApp')

app.controller('MainCtrl',function($scope,MainPage,$rootScope){
$scope.mainpage = MainPage.query();
    });

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

Upvotes: 2

Views: 5694

Answers (1)

Eliran Malka
Eliran Malka

Reputation: 16263

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);

      expect(scope.mainpage).toEqual(someMainPageMock);
    });
  });
});

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.

Upvotes: 2

Related Questions