Prabaharan Kathiresan Prabaharan Kathiresan - 5 months ago 700
AngularJS Question

Jasmine Test case for Angular UI grid onRegisterApi funtion

I'm writing Jasmine Unit test for AngularJS UI grid.
I have a below UI grid function in my controller
Code:

$scope.gridOption.onRegisterApi = function(gridApi) {
$scope.gridApi.core.on.sortChanged($scope, function(grid, sortColumns) {};
gridApi.pagination.on.paginationChanged($scope, function(newPage, pageSize) {};
};


I dont know how to write Unit test case for this function & internal private funtions....

Any one can help me on this?

Thanks in Advance!!!

Answer

You cannot unit tests methods that are not declared on the scope or accessible via properties. Some people use _ to namespace private methods.

And it makes sense - you're testing the final output, you're not testing the "internals" - internals are either external libs which are unit tested themselves, either private methods that you could push via different params of public methods (otherwise they their existence is questionable).

If external action simulation is your target, then just use spies: https://www.youtube.com/watch?v=0EXBpsz9Bwc

UPDATE - sample for service testing

describe('myService', function () {
  'use strict';

  beforeEach(function () {
    // module that we are testing
    module('appServices');
  });

  it('myMethodName - should return empty array if called with null', inject(function (myService) {
    expect(myService.myMethodName(null)).toBe([]);
  }));
});

Ctrl testing: https://www.youtube.com/watch?v=sZp9Hz3bHZE .

There's a full suite of videos there - just download/copy the code and run locally to see exactly the use-cases. Spies would basically allow you to hi-jack a request to an external service/reference and mock the response - so when your method calls that external service, instead of actually calling that service, it will instead return your response and therefore you can asses what should the method return or not.

Comments