Prabaharan Kathiresan Prabaharan Kathiresan - 1 year ago 1245
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

$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 Source

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:

UPDATE - sample for service testing

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

  beforeEach(function () {
    // module that we are testing

  it('myMethodName - should return empty array if called with null', inject(function (myService) {

Ctrl testing: .

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.