如何注入在app.run函数中使用的模拟angular服务?
How do you inject a mocked angular service that is used in the app.run function?
我已经在我的angular应用中添加了一些身份验证逻辑,并且对包装webapi的服务的初始调用是在app.run函数中执行的,像这样:
myApp.run(function ($rootScope, $location, myService) {
myService.getCurrentUser().then(function (promise) {
//reroute to either access denied or the start page when access is verified.
});
// register listener to watch route changes
$rootScope.$on("$routeChangeStart", function (event, next, current) {
//check access
});
});
在这之后,我所有的单元测试都失败了,因为我不知道在创建应用模块之前如何注入myService的模拟版本。我已经将服务和模拟服务分离到一个单独的模块中,这样我就可以在创建实际的应用程序之前创建它。
angular.mock.module('ServiceModule');
angular.mock.module('EArkivApp', function ($provide, myMockedService) {
$provide.value('myService', myMockedService);
});
这是不工作,然而,它抱怨"myMockedService"(这是ServiceModule的一部分)是一个未知的提供者。你对我该如何解决这个问题有什么好的建议吗?
你能给我们提供住宿吗?
同时,如果你使用的是Karma,那么你可以这样写:
/*global describe, beforeEach, module, inject, it, expect*/
describe('Controller: MyCtrl', function () {
'use strict';
// load the controller's module
beforeEach(module('myApp'));
var MyCtrl,
scope,
modalInstance;
// Initialize the controller and a mock scope
beforeEach(inject(function ($controller, $rootScope, myMockedService) {
scope = $rootScope.$new();
modalInstance = _$modal_.open({
templateUrl: 'views/my-template.html'
});
MyCtrl = $controller('MyCtrl', {
$scope: scope,
$myMockedService: myMockedService
});
}));
it('should be true', function () {
expect(true).toBe(true);
});
});
我通过使用约曼脚手架学会了这一点,顺便说一下:)
相关文章:
- 茉莉花和角度模拟 :模拟处理本地存储的服务
- 我们如何模拟我们的服务来测试控制器
- 模拟服务返回promise:无法读取未定义的属性
- AngularJS中的模拟服务
- 如何在同一个服务中模拟一个方法
- Angular单元测试,模拟使用$injector注入的服务
- 服务有一个模拟的服务依赖,它返回承诺,在测试中挣扎
- 如何使用sinon模拟$timeout服务
- 没有注入模拟服务,而是注入了实际服务
- 如何注入在app.run函数中使用的模拟angular服务?
- 如何在模拟Angular服务中从JSON fixture中提取数据
- 如何模拟带参数的Angular服务方法
- 如何在AngularAMD应用程序单元测试中模拟服务
- AngularJS 中的单元测试 - 模拟服务和承诺
- 你如何模拟 Angular 服务这是一个函数
- 用nock模拟soap服务
- 在AngularJS中模拟控制器单元测试的依赖服务
- AngularJS茉莉服务模拟
- AngularJS + jasmine+karma:在控制器中模拟服务响应
- 轻松模拟restful服务的工具