加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

在AngularJS / Jasmine中注入Mock服务

发布时间:2020-12-17 08:32:56 所属栏目:安全 来源:网络整理
导读:我正在使用茉莉来测试我在TypeScript中编写的控制器.我的单元测试是用普通的 javascript. 当我测试我的控制器时,我收到一个错误,我想注入一个模拟服务. 这是我的测试看起来的样子: 'use strict';describe('ConfigCtrl',function(){ var scope,http,location
我正在使用茉莉来测试我在TypeScript中编写的控制器.我的单元测试是用普通的 javascript.
当我测试我的控制器时,我收到一个错误,我想注入一个模拟服务.

这是我的测试看起来的样子:

'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);
    });
});

我的app.ts:

module game {
    'use strict';

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

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

}

和我的TypeScript控制器:

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
        }
    }   
}

当运行业力时,我收到以下错误:

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)

看来,注入configService存在问题,但我不知道为什么.

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

更新:似乎茉莉花在不同文件中具有TypeScript类的编译javascript是一个问题.将TypeScript文件编译为单个.js文件(tsc –out dest.js source.ts),对我来说是这样的.

尝试使用$injector获取服务.
beforeEach(angular.mock.inject(function($rootScope,$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',configService: service});
}));

链接到Demo.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读