使用茉莉花测试角度控制器 - 模块问题
Test angular controllers with Jasmine - module issue
我决定学习如何用Jasmine测试我的角度代码。当我不使用特定的依赖项时,一切都可以正常工作,但如果有一些依赖项,我就有问题。例如,我们有控制器.js:
angular.module('myApp', ['jmdobry.angular-cache'])
.controller('MyCtrl', ['$scope', '$http', function($scope, $http) {
$scope.myName = "Wojtek";
/...
}]);
现在我想测试:
describe('myApp', function() {
var scope,
controller;
beforeEach(angular.mock.module('myApp'));
describe('MyCtrl', function() {
beforeEach(inject(function ($rootScope, $controller) {
scope = $rootScope.$new();
controller = $controller('MyCtrl', {
'$scope': scope
});
}));
it('sets proper name', function () {
expect(scope.myName).toBe("Wojtek");
});
});
});
我的问题是 - 如何模拟"jmdobry.angular-cache"依赖关系?
由于在测试中不需要该模块的实际模拟功能,因此您可以执行以下操作:
describe('myApp', function () {
var scope,
controller;
beforeEach(angular.mock.module('jmdobry.angular-cache', [])); // just create a module that does nothing
beforeEach(angular.mock.module('myApp'));
describe('MyCtrl', function () {
beforeEach(inject(function ($rootScope, $controller) {
scope = $rootScope.$new();
controller = $controller('MyCtrl', {
'$scope': scope
});
}));
it('sets proper name', function () {
expect(scope.myName).toBe("Wojtek");
});
});
});
相关文章:
- 请参阅angularjs子模块中的控制器
- 将模块注入控制器AngularJs
- 角度如何解析模块中定义的控制器
- 如何通过不同的方式从控制器加载ui.bootstrap模块
- Titanium iOS提供can'找不到控制器的模块
- 角度控制器名称在模块之间冲突
- 在具有多个控制器的模块中实例化$scope
- 如何在angularjs中为每个控制器注入一个独特的模块
- Angularjs - 如何在 1 个控制器中使用模块
- AngularJS从不同的模块路由到控制器
- 从 Angular 控制器访问不同模块中的角度服务
- AngularJS - 如何从ui-router引用子模块控制器
- 使用茉莉花测试角度控制器 - 模块问题
- Angular - 如何在模块之间共享控制器
- 角度:需要从模块注入控制器
- 为什么 Angular Hint 建议控制器和模块的帕斯卡大小写名称
- Karma/Jasmine用控制器模块测试Angular
- 如何在angular JS中配置视图和控制器模块
- 如何获取/设置控制器/模块之外的变量
- angularjs控制器模块参考加载空白页