2

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.

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.