AngularJS,防止初始化方法在控制器启动茉莉花测试期间
问题描述:
我有一个控制器init()方法启动实例化。 它在一个真实的环境中做了一些对我的应用很有用的东西,但是这会让我的单元测试间谍更加混乱。AngularJS,防止初始化方法在控制器启动茉莉花测试期间
在单元测试环境中实例化控制器时,有没有办法阻止它的调用? 或者也许有办法让它在webapp上下文中自动调用,而无需在控制器代码的末尾显式调用init()?
答
在没有看到实时代码示例的情况下提供精确指导有点困难(这就是为什么提供一个具有Jasmine测试模板的plunk通常是个好主意),但它听起来像您的init
方法执行一些设置逻辑应该根据环境而有所不同。如果是这样,前进的方式是将这个初始化逻辑封装到一个专用服务中,并在测试期间模拟这个服务(这正是@Joe Dyndale所建议的)。
前提是你的控制器看起来像如下:
app.controller('MainCtrl', function($scope) {
$scope.init = function() {
//something I really don't want to call during test
console.log("I'm executing");
};
});
它可以被重构为:
app.factory('InitService', function() {
return {
init = function() {
//something I really don't want to call during test
console.log("I'm executing");
}
};
});
app.controller('MainCtrl', function($scope, InitService) {
InitService.init();
});
,然后用嘲讽的测试可能看起来像这样:
describe('Testing an initializing controller', function() {
var $scope, ctrl;
//you need to indicate your module in a test
beforeEach(module('plunker'));
beforeEach(module(function($provide){
$provide.factory('InitService', function() {
return {
init: angular.noop
};
});
}));
beforeEach(inject(function($rootScope, $controller) {
$scope = $rootScope.$new();
ctrl = $controller('MainCtrl', {
$scope: $scope
});
}));
it('should test sth on a controller', function() {
//
});
});
最后here是一款活跃代码
可以在init()中实际发生的实际情况中使用更多的上下文以获得适当的答案。根据这一点,你可以采取几种不同的路径。 – johlrich 2013-02-17 13:50:18
听起来像是转向服务或指令的候选人。如果代码可以移出控制器,它可以在单元测试中被模拟(在你的情况下可能有noop函数)。 – 2013-02-17 13:50:24