我使用控制器作为角度的语法,我想用茉莉花和sinon测试我的代码。测试javascript函数 - 对控制器不可见(角度)
比方说,我想下面的控制器代码:
angular
.module('Test')
.controller('TestController', TestController);
TestController.$inject = [];
function TestController() {
var viewModel = this;
viewModel.myFunction = myFunction;
function myFunction(){
//do something
//now call a helper function
helperFunction()
}
function helperFunction(){
// ....
}
}
我的问题是我怎么能测试helperFunction()或者甚至把间谍就可以了?我的帮手在我的测试中不可见。
这里是我的测试:
(function() {
'use strict';
var myController;
describe('Test', function() {
beforeEach(module('Test'));
beforeEach(inject(function ($controller, $injector) {
myController = $controller('TestController');
}));
it('Tests helperFunction', function(){
var sinonSpy = sinon.spy(myController, 'helperFunction');
//perform the action
myController.myFunction();
//expect the function was called - once
expect(sinonSpy .callCount).toEqual(1);
}
})
})