单元测试 – AngularJS – 具有值依赖注入的单元测试服务
发布时间:2020-12-17 17:30:44 所属栏目:安全 来源:网络整理
导读:我目前正在试验AngularJS.我创建了一个简单的服务,它是从一些远程数据初始化的. url作为应用程序值传递. //App declarationangular.module('myApp',['myApp.services']).value('someUrl','http://www.example.com');//Service declarationangular.module('my
我目前正在试验AngularJS.我创建了一个简单的服务,它是从一些远程数据初始化的. url作为应用程序值传递.
//App declaration angular.module('myApp',['myApp.services']).value('someUrl','http://www.example.com'); //Service declaration angular.module('myApp.services',['someUrl']).factory('MyService',function(someUrl) { var data; "Do stuff" return data; }); 现在,我正在尝试对此进行单元测试,但我无法正确设置“someUrl”参数.我尝试过这样的事情,没有成功: angular.module('myApp','test/test.json'); describe('Services',function() { beforeEach(module('myApp.services')); describe('MyService',function() { it('should return {success: true}',inject(function() { expect(data).toEqual({success: true}); })); }); }); 但是,我总是收到“No module:someUrl”错误.在单元测试期间初始化app值的正确语法是什么? 解决方法
您将值设置为“myApp”模块并正在测试“myApp.services”模块.您应该将模块(‘myApp.services’)更改为模块(‘myApp’).
无论如何,有一个更好的方法.您可以使用模拟模块注入$provide,以便您可以覆盖任何依赖项. 这是您的规格看起来像: describe('Services',function() { // here we load the module myApp and load an additional one that // allows you to override your dependency beforeEach(module('myApp',function($provide) { $provide.value('someUrl','test/test.json'); })); describe('MyService',function() { it('should return {success: true}',inject(function() { expect(data).toEqual({success: true}); })); }); }); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |