angularjs - Unit-testing with Karma-Jasmine -


i'm using angularjs , understand how test $scope objects karma-jasmine i'm having difficulties testing regular functions , variables inside controller file

//controller.js angular.module('myapp').controller('mainctrl', function ($scope) {     $scope.name = "bob";      var anumber = 34;      function myfunction(string){       return string;     } }); 

what test see if expect(anumber).tobe(34);

// test.js describe('controller: mainctrl', function () {    // load controller's module   beforeeach(module('myapp'));    var mainctrl,     scope;    // initialize controller , mock scope   beforeeach(inject(function ($controller, $rootscope) {     scope = $rootscope.$new();     mainctrl = $controller('mainctrl', {       $scope: scope     });   }));    // understand   it('should expect scope.name bob', function(){    expect(scope.name).tobe('bob');   });    // having difficulties testing   it('should expect anumber 34', function(){     expect(anumber).tobe(34);   });    // having difficulties testing       it('should return string', function(){     var mystring = myfunction('this string');     expect(mystring).tobe('this string');   });   });  

it looks trying test private variables declared in angular controller. variables not exposed through $scope cannot tested, hidden, , visible in function scope inside controller. more on private members , information hiding in javascript can find here

the way how should approach private fields in tests testing them through exposed api. if variable not used in exposed publicly method means it's not used doesn't make sense keep , test it.


Comments

Popular posts from this blog

Why can rails not find a route created by a helper? -

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -