gpt4 book ai didi

testing - 如何在没有 Mock 的情况下测试 Angular2/TypeScript HTTPService

转载 作者:太空狗 更新时间:2023-10-29 18:26:42 25 4
gpt4 key购买 nike

import {Injectable} from '@angular/core';
import {Http} from '@angular/http';

@Injectable()
export class HttpService {
result: any;

constructor(private http:Http) {
}

public postRequest(){
return this.http.get('http://httpbin.org/get');
}
}

上面是我的代码,这是我的测试:

不想模拟任何东西,只是测试真正的 http 连接。

编辑 - 新的 service.spec 文件:

import {beforeEachProviders, beforeEach, it, describe, expect, inject} from '@angular/core/testing';
import {HttpService} from '../../providers/http-service/http-service';
import {TranslateService} from 'ng2-translate/ng2-translate';
import {Goal} from '../../providers/goal/goal';
import {NavController} from 'ionic-angular';
import {HTTP_PROVIDERS, Http} from '@angular/http';

describe('Http Service Test', () => {

beforeEachProviders(() => {
return [
HTTP_PROVIDERS,
HttpService
];
});

it('should return response when subscribed to postRequest',
inject([HttpService], (httpService: HttpService) => {

httpService.postRequest().subscribe((res) => {
expect(res.text()).toBe('hello raja');
});
}));
});

这些是我在 karma 控制台中的错误:

28 06 2016 14:33:32.067:ERROR [Chrome 51.0.2704 (Mac OS X 10.11.4) | Http Service Test | should return response when subscribed to postRequest]: TypeError: Cannot read property 'getCookie' of null
at CookieXSRFStrategy.configureRequest (http://localhost:9876/absolute/var/folders/vy/18sb1wqs60g734bhr75cw9_r0000gn/T/9b9439f5f9c1590d3052594bcae9e877.browserify?26719cf22e6406ebc638b6b187c777666dcc5698:36568:81)
at XHRBackend.createConnection (http://localhost:9876/absolute/var/folders/vy/18sb1wqs60g734bhr75cw9_r0000gn/T/9b9439f5f9c1590d3052594bcae9e877.browserify?26719cf22e6406ebc638b6b187c777666dcc5698:36583:28)
at httpRequest (http://localhost:9876/absolute/var/folders/vy/18sb1wqs60g734bhr75cw9_r0000gn/T/9b9439f5f9c1590d3052594bcae9e877.browserify?26719cf22e6406ebc638b6b187c777666dcc5698:37476:20)

最佳答案

您首先需要为模拟 HTTP 后端配置提供程序:

describe('HttpService Tests', () => {
beforeEachProviders(() => {
return [
HTTP_PROVIDERS,
provide(XHRBackend, { useClass: MockBackend }),
HttpService
];
});

然后你可以这样使用模拟后端:

mockBackend.connections.subscribe(
(connection: MockConnection) => {
if (connection.request.url === 'file1.json') {
// Simulate an error
var err = new ResponseError();
err.status = 404;
connection.mockError(<Error>err);
} else {
// Simulate a successful response
connection.mockRespond(new Response(
new ResponseOptions({
body: ['i love angular2']
})));
}
});

httpService.postRequest().subscribe((res:Respsone) => {
expect(res.text()).toBe('hello raja');
});

编辑

如果你想测试真实连接,只使用经典的 HTTP_PROVIDERS:

describe('HttpService Tests', () => {
beforeEachProviders(() => {
return [
HTTP_PROVIDERS,
HttpService
];
});

编辑1

因为你的调用是异步的,你可以使用async:

it('should return response when subscribed to postRequest',
async(inject([HttpService], (httpService: HttpService) => {

httpService.postRequest().subscribe((res) => {
expect(res.text()).toBe('hello raja');
});
})));

关于testing - 如何在没有 Mock 的情况下测试 Angular2/TypeScript HTTPService,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38075835/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com