ค้นหาเว็บไซต์

typescript - Angular จำลองการเรียก HTTP หลายครั้ง


ฉันมีบริการหนึ่งซึ่งกำลังส่งค่ากลับของการรวม forkjoin ของหลายการเรียกใช้งาน http ฉันต้องการทดสอบสถานการณ์นี้

    class CommentService{
     addComments(){

let ob1 = Observable.of({});
    let ob2 = Observable.of({});
if(any condition)
        ob1 = {this.http.post('/url/1')};
if(any condition)
            ob2 = {this.http.post('/url/2'};
        return Observable.forkJoin(ob1,ob2)
           }
     }

ด้านบนคือคลาสบริการของฉัน ฉันจะทำการจำลองการเรียก HTTP ได้อย่างไร

describe("CommentService", () => {
  let httpClient: HttpClient;
  let httpTestingController: HttpTestingController;
  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [HttpClientModule, HttpClientTestingModule],
      providers: [CommentService]
    });

    httpClient = TestBed.get(HttpClient);
    httpTestingController = TestBed.get(HttpTestingController);
  });

  it('addComments() call with normal and gate', inject([CommentService], (service: CommentService) => {


    let cmts = service.addComments();

    const reqGateComment = httpTestingController.expectOne('/url/1');
    expect(reqGateComment.request.method).toEqual('POST');

    const reqFactComment = httpTestingController.expectOne('/url/2');
    expect(reqFactComment.request.method).toEqual('POST');

    reqGateComment.flush({});
    reqFactComment.flush({});


    httpTestingController.verify();

    cmts.subscribe(results=>{
       expect(results.length).toEqual(2);
    });

  }));


});

ฉันกำลังได้รับข้อผิดพลาดการทดสอบดังต่อไปนี้ การเรียกใช้ CommentService addFactsAndComments() กับ normal และ gate

ข้อผิดพลาด: คาดหวังหนึ่งคำขอที่ตรงตามเกณฑ์ "ตรงกับ URL:

 '/url/1", found none.

คำตอบ #1

ฉันเคยมีปัญหาคล้ายกันและฉันแก้มันโดยใช้ fakeAsync

it('addComments() call with normal and gate', fakeAsync( inject([CommentService], (service: CommentService) => {

    service.addComments().subscribe(results=>{
        expect(results.length).toEqual(2);
     });

    const reqGateComment = httpTestingController.expectOne('/url/1');
    expect(reqGateComment.request.method).toEqual('POST');
    reqGateComment.flush({});

    tick(10000);

    const reqFactComment = httpTestingController.expectOne('/url/2');
    expect(reqFactComment.request.method).toEqual('POST');
    reqFactComment.flush({});


    httpTestingController.verify();

  })));

คำตอบ #2

นั่นเป็นเพราะคุณกำลังห่อ observables ที่สร้างโดย HttpClient ในตัวใหม่โดยใช้วิธี of.

โดยการทำ

joined$ = forkJoin(obs$(post1$),obs$(post2$))

หมายเหตุ: $ หมายถึง สิ่งที่สังเกตได้

คุณสร้าง observable ใหม่ที่:

  • สมัครสมาชิกกับ obs$ ภายนอก
  • รอให้พวกเขาทำเสร็จ
  • เก็บค่าล่าสุดที่พวกเขาส่งออกมา (ในกรณีนี้คือ post1$ และ post2$)
  • ส่งกลับค่าที่รวบรวมมาเป็นอาเรย์ตามลำดับของสตรีมต้นทาง

เนื่องจากเราได้สมัครใช้เฉพาะ obs$ ด้านนอก การทดสอบของคุณจึงล้มเหลวกับ

'/url/1', ไม่พบ

เนื่องจากเราไม่เคยสมัครรับโพสต์ภายใน หมายความว่าคำขอไม่ได้ถูกส่ง

เปลี่ยนวิธีการให้บริการของคุณเป็น:

addComments(){    
    const ob1 = this.http.post('/url/1');
    const ob2 = this.http.post('/url/2');

    return Observable.forkJoin(ob1,ob2);
 }

บทความที่เกี่ยวข้อง