【问题标题】:How test request http with parameters with jest - angular如何使用 jest 的参数测试请求 http - 角度
【发布时间】:2020-05-15 06:12:28
【问题描述】:

我正在使用 Jest 测试 Angular 服务。

我可以模拟我的 httpClient,但我有一个问题是我无法使用参数测试 url(例如:“http://localhost:8080/api/feature/num?id=25663”)。

这是我的测试代码:

describe('MyService', () => {
   let myService: MyService;
   const httpMock = {get: jest.fn(() => of(dataMock))};
   const provide = (mock: any): any => mock;

   beforeEach(() => {
      myService= new MyService(provide(httpMock));
   });

   test('should created', () => {
     expect(myService).toBeTruthy();
   });

   test('should retrieve one user', (done) => {
     const number = 26586;
     const url = 'http://localhost:8080/api/feature/num?number=25663';

     myService.getNumero(number).subscribe( 
       res => {
          expect(httpMock.get).toBeCalledWith(url);
          expect(res.number).toBe(number);
          expect(res.city).toEqual('PARIS');
          done();
     });
   });
});

在我的控制台中我有这个错误:

Error: Uncaught [Error: expect(jest.fn()).toBeCalledWith(...expected)

Expected: "http://localhost:8080/api/feature/num?number=25663"
Received: "http://localhost:8080/api/feature/num, {
 "params": {
    "cloneFrom": {
        "cloneFrom": null, 
        "encoder": {}, 
        "map": null, 
        "updates": null
     },
     "encoder": {}, 
     "map": null,
     "updates": [{"op": "a", "param": "number", "value": "25663"}]
   }
 }

【问题讨论】:

    标签: javascript angular unit-testing jestjs angular-test


    【解决方案1】:

    通常我喜欢这样

    import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
    

    ...

    describe('MyService', () => {
        let injector: TestBed;
        let httpMock: HttpTestingController;
    
        beforeEach(() => {
            TestBed.configureTestingModule({
                imports: [HttpClientTestingModule],
                providers: [],
            });
            injector = getTestBed();
            httpMock = injector.get(HttpTestingController);
        });
    
        afterEach(() => {
            httpMock.verify();
        });
    
    

    ...

        it('should return number and city', () => {
            const expectedResult: {
                number: 26586,
                city: 'PARIS'
            };
    
            const service : MyService = TestBed.get(MyService);
            service.getNumero(number)
                .pipe(
                    first(),
                )
                .subscribe((data) => {
                    expect(data).toMatchObject(expectedResult);
                });
    
            const req = httpMock.expectOne(`http://localhost:8080/api/feature/num?number=25663`);
            expect(req.request.method).toBe('GET');
            req.flush(expectedResult);
        });
    
    

    【讨论】:

      猜你喜欢
      • 2021-02-21
      • 2020-07-25
      • 1970-01-01
      • 2018-08-25
      • 1970-01-01
      • 2014-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多