问题
I am using Karma test runner and Jasmine. I know how to test functions on the scope.But in case of a situation like this how do I go about it ?
listing_app.controller('my_listing_products_list', ['$scope', '$modal',
function ($scope, $modal) {
this.someFn = function(a,b){
//How do i test this function ?
}
}]);
How do I get hold of the this object and the controller context through Jasmine ?
回答1:
Try the following:
describe('my_listing_products_list controller', function(){
beforeEach(inject(function($controller, $rootScope){
scope = $rootScope.$new();
ctrl = $controller("my_listing_products_list", {$scope: scope});
}));
it('should exist', function($controller){
expect(ctrl.someFn()).toBe('whatever the function returns')
});
})
来源:https://stackoverflow.com/questions/20910445/testing-functions-not-on-the-scope-in-the-controller