1

We are building an app that is using the withCredentials flag. Referencing this question, we were able to use the withCredentials flag. Now I'm trying to unit test the service with a mockbackend. I was able to get everything working as long as I remove the withCredentials flag.

My service:

@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;
     };
  }
}

My spec file:

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');
    });
});

I'm guessing the problem is because the withCredentials flag isn't mocked in my mockbackend. So how do I do this? Or can I set up my http withCredentials flag in a different way?

The error:

     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
Community
  • 1
  • 1
Aarmora
  • 1,143
  • 1
  • 13
  • 26

0 Answers0