Testing functions not on the scope in the controller

两盒软妹~` 提交于 2019-12-08 09:37:27

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!