3
は、テストをしようが、このエラーメッセージが出ます:オブジェクトの種類もするテストHTTPが動作しない
予想されるオブジェクトを、しかし、ステータスと応答した:URL用 はnullはnull:ヌル。
何が不足していますか?
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);
}
試験:
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"
});
});
})));
});
});
は、あなたがこれを解決しました:、あなたが.json()メソッドを呼び出す必要があり
は、要求(リクエストのすなわち体)の内部のデータにアクセスするには? – fsi