angularjs - 在 AngularJS/Jasmine 中注入(inject) Mock 服务

标签 angularjs typescript jasmine

我正在使用 jasmine 来测试我用 TypeScript 编写的 Controller 。我的单元测试是用普通的 javascript 编写的。 我在测试我的 Controller 时遇到错误,我想在其中注入(inject)模拟服务。

这是我的测试结果:

'use strict';

describe('ConfigCtrl', function(){
    var scope, http, location, timeout, $httpBackend, service;

    beforeEach(angular.mock.module('busybee'));

    beforeEach(angular.mock.inject(function($rootScope, $http, $location, $timeout, configService, $controller){

        scope = $rootScope.$new();
        http = $http;
        location = $location;
        timeout = $timeout;
        service = configService;


        $controller('configCtrl', {$scope: scope, $http: http, $location: location, $timeout: timeout, configService: service});
    }));

    it('should have text = "constructor"', function(){
        expect(true).toBe(true);
    });
}); 

我的应用程序:

module game {
    'use strict';

    var busybee = angular.module('busybee', []);
    busybee.controller('configCtrl', ConfigCtrl);

    busybee.service('configService', ConfigService);
    ...
    ...

}

和我的 TypeScript Controller :

module game {
    'use strict';

    export class ConfigCtrl {

        static $inject: string[] = ['$scope', '$http', '$location', '$timeout', 'configService'];

        constructor($scope: ng.IScope, $http: ng.IHttpService, $location: ng.ILocationService,
            $timeout: ng.ITimeoutService, configService: game.ConfigService) {  
            //any code here
        }
    }   
}

运行 karma 时,出现以下错误:

Chrome 28.0.1500 (Linux) ConfigCtrl should have text = "constructor" FAILED
        TypeError: Cannot read property 'prototype' of undefined
            at Object.instantiate (/home/david/git/busybee2-client/js/libs/angular/angular.min.js:28:283)
            at Object.<anonymous> (/home/david/git/busybee2-client/js/libs/angular/angular.min.js:28:494)
            at Object.d [as invoke] (/home/david/git/busybee2-client/js/libs/angular/angular.min.js:28:174)
            at /home/david/git/busybee2-client/js/libs/angular/angular.min.js:29:339
            at c (/home/david/git/busybee2-client/js/libs/angular/angular.min.js:27:13)
            at Object.d [as invoke] (/home/david/git/busybee2-client/js/libs/angular/angular.min.js:27:147)
            at workFn (/home/david/git/busybee2-client/js/libs/angular/angular-mocks.js:1778:20)
        Error: Declaration Location
            at Object.window.jasmine.window.inject.angular.mock.inject [as inject] (/home/david/git/busybee2-client/js/libs/angular/angular-mocks.js:1764:25)
            at null.<anonymous> (/home/david/git/busybee2-client/js/test/ConfigCtrlSpecs.js:9:29)
            at /home/david/git/busybee2-client/js/test/ConfigCtrlSpecs.js:3:1
Chrome 28.0.1500 (Linux): Executed 1 of 1 (1 FAILED) ERROR (0.329 secs / 0.032 secs)

似乎注入(inject) configService 时出现问题,但我不知道为什么。

编辑:添加了一个 jsfiddle http://jsfiddle.net/Q552U/6/

更新:对于 jasmine 在不同文件中具有 TypeScript 类的已编译 javascript 而言,这似乎是一个问题。将 TypeScript 文件编译为单个 .js 文件 (tsc --out dest.js source.ts),帮我完成了。

最佳答案

尝试使用 $injector 获取服务。

beforeEach(angular.mock.inject(function($rootScope, $http, $location, $timeout, configService, $controller, $injector){
    scope = $rootScope.$new();
    http = $http;
    location = $location;
    timeout = $timeout;
    service = $injector.get('configService'); //not sure the name, you may try 'ConfigService' as well.

    $controller('configCtrl', {$scope: scope, $http: http, $location: location, $timeout: timeout, configService: service});
}));

链接到 Demo .

关于angularjs - 在 AngularJS/Jasmine 中注入(inject) Mock 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18469408/

相关文章:

javascript - zone.js 和 Jasmine 的时钟之间的冲突

angular - 如何在 Angular 中不使用方法 stub 导入的模块?

javascript - 使用angularjs提取字节数组中的多个文件内容

javascript - 让指令监听函数

Angularjs Kendo Treeview 未在 Kendo 弹出窗口中显示复选框

javascript - Angular 5 组件变量在单页应用程序中未定义,无需刷新浏览器

javascript - 如何使用 Jasmine 和 Node 从命令行测试 jQuery 插件?

javascript - 如何在 Angular 中为我的页面上具有特定值或属性的 div 的数量返回一个计数?

javascript - 如何使用jscodeshift在节点添加 'await'?

javascript - 在对象中存储接口(interface)