Angular2 Jasmine spyOn http调用

Angular2 Jasmine spyOn http调用,angular,jasmine,angular2-http,angular2-testing,Angular,Jasmine,Angular2 Http,Angular2 Testing,我想监视Http服务,看看它是否调用了正确的端点。有可能吗 到目前为止,在我的服务中,我测试的只是他们发送回数据。。。我想让它们更有用 如果您需要它,以下是我当前的规范文件: import { TestBed, async, inject } from '@angular/core/testing'; import { SignupService } from './signup.service'; import { Observable } from 'rxjs/Observable'; i

我想监视
Http
服务,看看它是否调用了正确的端点。有可能吗

到目前为止,在我的服务中,我测试的只是他们发送回数据。。。我想让它们更有用

如果您需要它,以下是我当前的规范文件:

import { TestBed, async, inject } from '@angular/core/testing';
import { SignupService } from './signup.service';
import { Observable } from 'rxjs/Observable';

import { HttpModule, Http, Response, ResponseOptions, RequestOptions, Headers, XHRBackend } from '@angular/http';
import { MockBackend, MockConnection } from '@angular/http/testing';
import { ErrorHandlerService } from '../../services/error-handler.service';

describe('SignupService', () => {

  let errorStub = {};

  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [HttpModule],
      providers: [
        SignupService,
        { provide: XHRBackend, useClass: MockBackend },
        { provide: ErrorHandlerService, useValue: errorStub }
      ]
    });
  });

  it('signup should return an application error if no apps are provided', inject([SignupService], (service: SignupService) => {
    service.signup('', [], null).subscribe(
      suc => { },
      err => expect(err).toMatch(/application/)
    );
  }));

  it('signup should return a role error if no roles are provided', inject([SignupService], (service: SignupService) => {
    service.signup('', null, []).subscribe(
      suc => { },
      err => expect(err).toMatch(/rôle/)
    );
  }));

  it(
    'signup should return a response with a "contenu" parameter',
    inject([SignupService, XHRBackend], (service: SignupService, mockBackEnd: MockBackend) => {
      let content = 'anything';
      mockBackEnd.connections.subscribe((connection: MockConnection) => {
        connection.mockRespond(new Response(new ResponseOptions({
          body: JSON.stringify({
            contenu: content
          })
        })));
      });
      service.signup('', [], []).subscribe(
        suc => expect(suc).toBe(content)
      );
    }));

  it(
    'checkUser should return a response with a "contenu" parameter',
    inject([SignupService, XHRBackend], (service: SignupService, mockBackEnd: MockBackend) => {
      let content = 'anything';
      mockBackEnd.connections.subscribe((connection: MockConnection) => {
        connection.mockRespond(new Response(new ResponseOptions({
          body: JSON.stringify({
            contenu: content
          })
        })));
      });
      service.checkUser('').subscribe(
        suc => expect(suc).toBe(content)
      );
    }));
});

快到了!只需在mockBackend声明中添加expect

例如:

mockBackend.connections.subscribe(连接=>{
//检查它是否调用http POST调用
expect(connection.request.method).toBe(RequestMethod.Post);
//检查URL是否正确
expect(connection.request.url).toBe('/someurl');
连接。mockresponse(新响应)(新响应选项({
正文:JSON.stringify(“”),
现状:200
})));
});

Wow,我去上班休息了一周,完全忘记了那个帖子(我已经放弃了,决定不测试它)。你的解决方案确实有效,这太神奇了!谢谢你,伙计