使用模拟后端测试HTTP [英] Testing HTTP with mockbackends
问题描述
我正在为Angular 2中的服务构建测试.事实证明,构建模拟后端是一个真正的尝试.我已经能够成功发出实际的HTTP请求来测试服务,但我希望将它们与第三部分分开.
I'm working on building out tests for my services in Angular 2. Building out the mock backends is proving to be a real trial. I have been able to test the services with it making actual HTTP requests successfully but I would like to keep these segregated from the third part.
我仔细阅读了我能找到的两篇主要文章(第一,秒以及角度文档一个>.
I've combed through the two main articles I have been able to find (first, second as well as the angular docs.
当我向实际的第三方服务成功发出http请求时,必须使用done();
以确保测试等待请求完成.似乎这里的情况是一样的.如果我不使用它,即使使用expect(1).toBe(2);
,测试也将成功.当我使用它时,它会等待呼叫完成而超时.错误:
When I was making successful http requests to actual third party services, I had to use done();
to ensure that the test waited for the request to complete. It seems that the case is the same here. If I do not use it, the test will be a success even with expect(1).toBe(2);
. When I do use it, it times out waiting for the call to complete. Error:
Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
这是我的代码:
import {
it,
inject,
describe,
beforeEachProviders,
expect,
} from '@angular/core/testing';
import { BaseRequestOptions, Response, ResponseOptions, Http } from '@angular/http';
import { MockBackend, MockConnection } from '@angular/http/testing';
import { AccountService } from './../../source/scripts/services/account.service';
import { provide } from '@angular/core';
describe('AccountService', () => {
// let service;
beforeEachProviders(() => [
AccountService,
BaseRequestOptions,
MockBackend,
provide(Http, {
deps: [MockBackend, BaseRequestOptions],
useFactory: (backend: MockBackend, defaultOptions: BaseRequestOptions) => {
return new Http(backend, defaultOptions);
},
}),
]);
beforeEach(<any>inject([MockBackend], (backend: MockBackend) => {
const baseResponse = new Response(new ResponseOptions({ body: 'got response' }));
backend.connections.subscribe((c: MockConnection) => c.mockRespond(baseResponse));
}));
// We use 'done' for async callbacks (http calls)
it('should return mocked response', (done) => {
inject([AccountService], (testService: AccountService) => {
testService.getUsers().subscribe((res: Response) => {
expect(res.text()).toBe('got response');
expect(1).toBe(2);
done();
});
});
});
});
如何正确地使用模拟后端测试http调用?我怀疑我上面引用的链接已经过时,或者它们没有确认测试实际上在测试,而不是在没有done();
异步的情况下仅给出误报.
How do I properly test http calls with a mockbackend? I suspect that either the links I referenced above are out of date or they didn't confirm that the test was actually testing rather than just giving false positives without the async of done();
.
推荐答案
您非常亲密.这里仅异步done
不适用,因为inject
必须替换此匿名回调.所以这是固定版本:
You were very close. Only the async done
is not appropriate here because inject
has to replace this anonymous callback. So here is the fixed version:
it('should return mocked response', inject([AccountService], (testService: AccountService) => {
testService.getUsers().subscribe((res: Response) => {
expect(res.text()).toBe('got response');
expect(1).toBe(2);
});
}));
另一个问题缺少beforeEach
导入:
import {
it,
inject,
describe,
beforeEach, // <-- this one was missing
beforeEachProviders,
expect,
} from '@angular/core/testing';
因此有一个Jasmine的beforeEach
实例,不允许使用inject
.
So there was a Jasmine's beforeEach
instance which doesn't allow to use inject
.
这篇关于使用模拟后端测试HTTP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!