mercredi 8 avril 2015

AngularJs unit testing scope variable(array) inside controller is undefined?

I am writing unit testing in angularJS for controllers.


I am testing for each scope variable defined inside controller.


This particular error is occurring only for array scope variable.


Other than that I can test dictionary, string ,Boolean variables and also functions successfully.


Please tell me how to test an array scope variable inside controller?


Code for controller:



ctrls.controller('catalogController', ['$scope', '$state',
'utilService', 'products',
'catalogFactory',
'notificationFactory',
'$rootScope',
'$location',function($scope){
$scope.productList = [];
}
);


Test case code for controller:



describe('TestingcatalogController',
function() {
var $controller,
scope,
rootScope,
state,
notificationFactory,
createUserData,
deferred,
utilService,
products,
catalogFactory,
location;
beforeEach(function() {
module('gem.catalog.controllers');
module('gem.fixture.catalogs');
module('gem.mock.state');
module('gem.mock.common.factories');
module('gem.mock.common.constants');
module('gem.mock.common.services');
module('gem.mock.catalogs.factories');
inject(function($rootScope, _$controller_, mockState, mockNotificationFactory, mockCatalogsFactory, listProducts, utilService) {
scope = $rootScope.$new();
rootScope = $rootScope.$new();
state = mockState;
notificationFactory = mockNotificationFactory;
catalogFactory = mockCatalogsFactory;
products = listProducts;
$controller = _$controller_('catalogController', {
$scope: scope,
$rootScope: rootScope,
products: listProducts,
$state: state,
catalogFactory: catalogFactory,
utilService: utilService,
notificationFactory: notificationFactory
}); //scope._private_.pushToErrorMessage=spyOn(scope._private_,
'pushToErrorMessage').and.callThrough();
});
});
describe('testallvariables', function() {
it('shouldhaveproductListvariableinaScope', function() {
expect(scope.productList).not.toBeNull();
expect(scope.productList).toEqual([]);
});
});
});


Error:



Chrome 41.0.2272 (Linux) Testing catalogController test all variables should have productList variable in a Scope FAILED
Expected undefined to equal [ ].
at Object.<anonymous> (/home/vikrant/project/gem-bo/test/unit/catalogs/controllers.js:72:28)
Chrome 41.0.2272 (Linux): Executed 258 of 258 (1 FAILED) (1.124 secs / 1.056 secs)

Aucun commentaire:

Enregistrer un commentaire