Jasmine单元测试:注入http服务

时间:2016-04-26 12:33:22

标签: angularjs typescript jasmine karma-runner

我有下一个服务“IrChartService”:

public getDataAfterUpdate(entityId: number, timestamp: number) {
    var deferred = this.$q.defer();
    var requestUrl = this.IrChartData + '/datasequences/entities/' + entityId + '/ir?timestamp=' + timestamp;

    this.$http.get(requestUrl).then((result) => {
        deferred.resolve(result.data);
    });

    return deferred.promise;

}

该服务用于控制器。 我在这里有测试文件:

  /* Test Code */
    describe('IrChartService', function () {
        var IrChartService, $httpBackend, $q, IrChartData;

        beforeEach(function(){
            angular.module('genscape.rt.irchart')
        });
        beforeEach(inject(function(_$httpBackend_, _IrChartService_, _$q_, _IrChartData_) {

            IrChartService = _IrChartService_;
            $httpBackend = _$httpBackend_;
            $q = _$q_;
            IrChartData = _IrChartData_;
        }));

        it('is defined', function () {
            console.log('def');
            expect(IrChartService).toBeDefined();
        });

        it('should call the backend testurl',  function() {
            var returnData = {
                "ImagePath": "/20160406/167/167_20160406_065626.jpg",
                "ConfidenceValue": 4
            };
            console.log('test');
            //7. expectGET to make sure this is called once.
            $httpBackend.expectGET("../mock/chartData.specs.json").respond(returnData);

            $httpBackend.flush();

        });
    });

它给我的错误如下: 错误:[$ injector:unpr] http://errors.angularjs.org/1.4.0-rc.2/ $ injector / unpr?p0 = IrChartServiceProvider%20%3C-%20IrChartService

TypeError:无法读取未定义的属性'expectGET'

预定未定义的定义。

你能帮助我吗?谢谢!

更新

// Karma configuration
// Generated on Wed May 20 2015 15:52:13 GMT-0400 (Eastern Daylight Time)

module.exports = function (config) {
    config.set({

        // base path that will be used to resolve all patterns (eg. files, exclude)
        basePath: '',

        // frameworks to use
        // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
        frameworks: ['jasmine'],

        plugins: [
            'karma-chrome-launcher',
            'karma-jasmine',
            'karma-html-reporter',
            'karma-ng-html2js-preprocessor'
        ],

        // list of files / patterns to load in the browser
        files: [
             'Scripts/angular.min.js',
             'Scripts/angular-mocks.js',
             'Scripts/genscape.common.js',
             'app/template.module.js',
             'app/app.module.js',
             'app/templates.js',
             'app/app.widget.service.js',
             'app/IRchart/IRchart.service.js',
             'app/IRchart/IRchart.controller.js',
             'app/IRchart/IRchart.directive.js',
             'app/IRchart/IRchart-error.directive.js',
             'app/IRchart/IRchart-popup/IRchart-popup.controller.js',
             'app/IRchart/IRchart-player/IRchart-player.controller.js',
             'app/IRchart/IRchart-player/IRchart-player.directive.js',
             'test/specs/specs.js',

              // fixtures
             { pattern: 'test/mock/*.json', watched: true, served: true, included: false }
        ],

        preprocessors: {
            '**/*.html': ['ng-html2js']
        },

        ngHtml2JsPreprocessor: {
            // we want all templates to be loaded in the same module called 'templates'
            moduleName: 'genscape.rt.irchart'
        },

        // list of files to exclude
        exclude: [
        ],

        // test results reporter to use
        // possible values: 'dots', 'progress'
        // available reporters: https://npmjs.org/browse/keyword/karma-reporter
        reporters: ['progress', 'html'],


        // web server port
        port: 9876,


        // enable / disable colors in the output (reporters and logs)
        colors: true,


        // level of logging
        // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
        logLevel: config.LOG_INFO,


        // enable / disable watching file and executing tests whenever any file changes
        autoWatch: true,


        // start these browsers
        // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
        //browsers: ['Chrome'],
        browsers: ['Chrome'],


        // Continuous Integration mode
        // if true, Karma captures browsers, runs the tests and exits
        singleRun: false
    });
};

0 个答案:

没有答案