注入$http在使用typescript的jasmine测试中失败
Injecting $http fails in jasmine testing with typescript
我们正在开发一个用Typescript测试驱动的程序。这是我们对茉莉花的测试:
import IDifferentialPilotService = DifferentialPilot.IDifferentialPilotService;
import Ev3DifferentialPilotService = DifferentialPilot.Ev3DifferentialPilotService;
import IHttpService = ng.IHttpService;
describe("restClientService integration test: ", function () {
// sut
var ev3DifferentialPilotService: IDifferentialPilotService;
//doc
var http: IHttpService;
beforeAll(function () {
angular.mock.inject(function ($http: IHttpService) {
this.http = $http;
});
ev3DifferentialPilotService = new Ev3DifferentialPilotService(http);
});
it("robot should run 5 m", function () {
expect(ev3DifferentialPilotService.runDistance(5)).toBe("success");
});
});
被测试的类Ev3DifferentialPilotService
看起来像这样:
namespace DifferentialPilot {
"use strict";
import IHttpService = ng.IHttpService;
export class Ev3DifferentialPilotService implements IDifferentialPilotService {
private http: IHttpService;
static $inject = ['$http'];
constructor($http: IHttpService) {
this.http = $http;
}
public runDistance(runDistance: number): string {
this.http({
method: "POST",
url: "10.0.0.44:8080/differentpilot/run/5"
}).then(function successCallback(response: any) {
return "success";
}, function errorCallback(response: any) {
return "error";
});
return undefined;
}
}
}
正如您所看到的,我们希望在测试中注入$http
-服务,以便Ev3DifferentialPilotService
可以检索Http服务作为参数,并在其runDistance
-方法中使用它。当我们在Ev3DifferentialPilotService
中记录this.$http
-对象时,我们得到undefined
,这意味着注入失败。据此,测试也失败了。
我正在App.js
:中将Ev3DifferentialPilotService
注册为服务
var app = angular.module("AngularDifferentialPilotModule", []);
app.service("ev3DifferentialPilotService", DifferentialPilot.Ev3DifferentialPilotService);
我们在karam.conf.js:中使用了所有这些东西
// Karma configuration
// Generated on Thu Dec 17 2015 14:02:38 GMT+0100 (Mitteleuropäische Zeit)
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'],
// list of files / patterns to load in the browser
files: [
"bower_components/angular/angular.js",
"bower_components/angular-mocks/angular-mocks.js",
"src/Ev3DifferentialPilotService.js",
"spec/*.spec.js",
"src/app.js"
],
// list of files to exclude
exclude: [],
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {},
// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress'],
// 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: false,
// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['Firefox'],
// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: false,
// Concurrency level
// how many browser should be started simultanous
concurrency: Infinity
})
}
此外,我们使用的tsconfig看起来像这样:
{
"compilerOptions": {
"module": "commonjs",
"noImplicitAny": true,
"removeComments": true,
"preserveConstEnums": true,
"sourceMap": true
}
那么错误在哪里呢?有人能帮我吗?
不能在beforeAll
中注入(https://github.com/angular/angular.js/issues/10238)
功能已打开(https://github.com/angular/angular.js/pull/14093)但尚未合并。
相关文章:
- 如何使用Jasmine测试Knockout.js点击绑定
- 如何在jasmine测试中为有角度的项目注入依赖项
- Jasmine:测试setTimeout函数会抛出一个错误
- 如何通过 phantomjs 可靠地执行利用 requirejs 的 Jasmine 测试
- 使用jasmine测试JavaScript回调函数
- Jasmine测试工厂函数调用本地函数和另一个函数-获取错误:应该是间谍,但得到了function
- 用jasmine测试JavaScriptUI的最佳方法
- 如何使用jasmine测试done和failedDeferredObject
- 在CoffeeScript文件上运行Jasmine测试时,对象未定义错误
- 使用 Jasmine 测试主干视图时,浏览器页面不断刷新
- 使用 Jasmine 测试对象中的事件处理程序
- 在 Jasmine 测试中无法访问 Angular 服务函数
- 如何正确引用Jasmine测试夹具中的图像
- jasmine测试抱怨undefined不是一个对象
- 我怎么能用jasmine测试这个代码呢
- 如何使用jasmine测试ajax响应
- 如何模拟ajax对Jasmine测试的调用
- AngularJS Jasmine测试:类型错误:'未定义'不是对象
- Jasmine测试url的javascript函数
- 使用Jasmine测试TypeScript类