我们正在构建一个使用withCredentials标志的应用。引用this问题,我们可以使用withCredentials标志。现在我试图用mockbackend对服务进行单元测试。只要我删除withCredentials标志,我就能使一切正常。
我的服务:
@Injectable()
export class AccountService {
public productParams;
constructor(
private http: Http) {
// This allows the cookie to be set and sent.
// It appears that once the cookie is set with login, it works for each call (even in other services)
let _build = (<any>http)._backend._browserXHR.build;
(<any>http)._backend._browserXHR.build = () => {
let _xhr = _build();
_xhr.withCredentials = true;
return _xhr;
};
}
}
我的spec文件:
let service;
beforeEachProviders(() => [
AccountService,
BaseRequestOptions,
MockBackend,
provide(Http, {
deps: [MockBackend, BaseRequestOptions],
useFactory: (backend: MockBackend, defaultOptions: BaseRequestOptions) => {
return new Http(backend, defaultOptions);
},
}),
]);
beforeEach(inject([AccountService, MockBackend], (s: AccountService, backend: MockBackend) => {
service = s;
const baseResponse = new Response(new ResponseOptions({ body: 'got response' }));
backend.connections.subscribe((c: MockConnection) => c.mockRespond(baseResponse));
}));
it('should return mocked response', () => {
service.getUsers().subscribe((res: Response) => {
expect(res.text()).toBe('got response');
});
});
我猜测问题是因为withCredentials标志在我的模拟后端没有被嘲笑。那我该怎么做?或者我可以用不同的方式设置http
withCredentials标志吗?
错误:
6) should return mocked response
AccountService
_instantiateProvider@C:/sites/Scratch/prototype_ui/config/karma-test-shim.js:18071:38 <- webpack:///~/@angular/core/src/di/reflective_injector.js:626:0
_new@C:/sites/Scratch/prototype_ui/config/karma-test-shim.js:18060:42 <- webpack:///~/@angular/core/src/di/reflective_injector.js:615:0
getObjByKeyId@C:/sites/Scratch/prototype_ui/config/karma-test-shim.js:17715:55 <- webpack:///~/@angular/core/src/di/reflective_injector.js:270:0
_getByKeyDefault@C:/sites/Scratch/prototype_ui/config/karma-test-shim.js:18240:52 <- webpack:///~/@angular/core/src/di/reflective_injector.js:795:0
_getByKey@C:/sites/Scratch/prototype_ui/config/karma-test-shim.js:18212:42 <- webpack:///~/@angular/core/src/di/reflective_injector.js:767:0
get@C:/sites/Scratch/prototype_ui/config/karma-test-shim.js:18021:31 <- webpack:///~/@angular/core/src/di/reflective_injector.js:576:0
C:/sites/Scratch/prototype_ui/config/karma-test-shim.js:26166:75 <- webpack:///~/@angular/core/testing/test_injector.js:46:46
map@[native code]
execute@C:/sites/Scratch/prototype_ui/config/karma-test-shim.js:26166:33 <- webpack:///~/@angular/core/testing/test_injector.js:46:0
C:/sites/Scratch/prototype_ui/config/karma-test-shim.js:26255:63 <- webpack:///~/@angular/core/testing/test_injector.js:135:28
C:/sites/Scratch/prototype_ui/config/karma-test-shim.js:26480:29 <- webpack:///~/@angular/core/testing/testing.js:98:0 _instantiate@C:/sites/Scratch/prototype_ui/config/karma-test-shim.js:18197:98 <- webpack:///~/@angular/core/src/di/reflective_injector.js:752:0
14 06 2016 02:36:45.593:WARN [reporter]: SourceMap position not found for trace: TypeError: undefined is not an object (evaluating 'service.getUsers') in http://localhost:9867/base/config/karma-test-shim.js?e346472b463b1f48b98add9734c44e8d5946fda0 (line 44942)
http://localhost:9867/base/config/karma-test-shim.js?e346472b463b1f48b98add9734c44e8d5946fda0:44942:17
http://localhost:9867/base/config/karma-test-shim.js?e346472b463b1f48b98add9734c44e8d5946fda0:26480:29
TypeError: undefined is not an object (evaluating 'service.getUsers') in C:/sites/Scratch/prototype_ui/config/karma-test-shim.js (line 44942)
C:/sites/Scratch/prototype_ui/config/karma-test-shim.js:44942:17 <- webpack:///spec/services/account.service.spec.ts:26:0
C:/sites/Scratch/prototype_ui/config/karma-test-shim.js:26480:29 <- webpack:///~/@angular/core/testing/testing.js:98:0