3

Try to make a test, but get this error message:

Expected object to be a kind of Object, but was Response with status: null null for URL: null.

Any idea what's missing here?

login.component.ts:

loginEvent(username, passw) {
      let obj = {
          user: username,
          pass: passw
      };

  if (this.validation(obj)) { 
      this.httpService.loginPostToServer(obj).subscribe(
          (response) => this.saveTokenToLocalstorage(response),
          (error) => console.log(error)
      );
    }
  };

http.service.ts:

loginPostToServer(data) {
    return this.http.post(this.url + 'login', data);
}  

Test:

import { async, TestBed, inject } from '@angular/core/testing';
import { HttpModule, Http, ResponseOptions, Response, BaseRequestOptions } from '@angular/http';
import { MockBackend } from '@angular/http/testing';
import { HttpService } from './http.service';

describe('HttpService', () => {
beforeEach(() => {
    TestBed.configureTestingModule({
    providers: [ HttpService, MockBackend, BaseRequestOptions,
    {
        provide: Http,
        useFactory: (backend, options) => new Http(backend, options),
        deps: [MockBackend, BaseRequestOptions]
    }],
    imports: [ HttpModule ]
    });
});

it('should be created', inject([HttpService], (service: HttpService) => {
    expect(service).toBeTruthy();
}));

it('should construct', async(inject(
    [HttpService, MockBackend], (service, mockBackend) => {
    expect(service).toBeDefined();
})));

describe('loginPostToServer', () => {
    const mockResponse = {
    success: true,
    token: "sample token",
    user: "john"
    };

    const fakeUser = {
    user: "john",
    pass: "1234"
    };

    it('should parse response', async(inject(
    [HttpService, MockBackend], (service, mockBackend) => {

    mockBackend.connections.subscribe(conn => {
        conn.mockRespond(new Response(new ResponseOptions({ body: JSON.stringify(mockResponse) })));
    });

    const result = service.loginPostToServer(fakeUser);

    result.subscribe(res => {
        expect(res).toEqual({
        success: true,
        token: "sample token",
        user: "john"
        });
    });
    })));
});
});
1
  • Did you solve this? Commented Sep 20, 2017 at 16:57

1 Answer 1

1

The problem is that the response object returned from the http call contains the data you want, but is not itself what you want.

To access the data inside of the request (i.e. the body of the request), you need to call the .json() method:

  if (this.validation(obj)) { 
      this.httpService.loginPostToServer(obj).subscribe(
          (response) => this.saveTokenToLocalstorage(response.json()), // .json() added here
          (error) => console.log(error)
      );
    }
  };
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.