【问题标题】:writing karma test case for post and get service in angular 8为帖子编写业力测试用例并以 Angular 8 获得服务
【发布时间】:2020-03-15 17:52:36
【问题描述】:

我是 Karma 的新手,不知道如何为 POST 方法编写测试用例。

这是我的 service.ts 文件:-

constructor(private httpclient: HttpClient) { }


  url = 'http://localhost:8047/some_url';

  check: Check[];

  public newBehaviour = new BehaviorSubject<Check[]>([]);

  postRents(hire, bookingId, trackingId): Observable<Check> {
    hire.bookingId = bookingId;
    hire.trackingId = trackingId;
    return this.httpclient.post<Check>(this.url, hire, {
      headers: new HttpHeaders({
        'Content-Type': 'application/json',
        'Access-Control-Allow-Origin': '*'
      })
    }).pipe(
      tap(data => {
        this.check.push(hire);
        this.newBehaviour.next(this.check);
        console.log(data);
      })
    );

我如何测试这段代码的 POST ?

编辑:- 无法为此测试 POST

postRents(hire, bookingId, trackingId): Observable<Check> {
        hire.bookingId = bookingId;
        hire.trackingId = trackingId;
        return this.httpclient.post<Check>(this.url, hire, {
          headers: new HttpHeaders({
            'Content-Type': 'application/json',
            'Access-Control-Allow-Origin': '*'
          })
        }).pipe(
          tap(data => {
            this.check.push(hire);
            this.newBehaviour.next(this.check);
            console.log(data);
          })
        );

方法实现出错

it('Check Serivice', () => {

  const mockData: RentBook[] = [{
    rentingId: 343,
    userName: 'dwew',
    phone: 242324,
    bookId: '3442',
    startDate: new Date(),
    endDate: new Date('Wed November 20 2019 00:00:00 GMT+0530 (India Standard Time)'),
   }];

  service.postRents(mockData, bookId: '34', rentId:34).subscribe(
      res => expect(res).toEqual(mockData)
    );
  httpTestingController.expectOne(req => req.url.includes('http://localhost:8047/renting-app/api/v1/rentings') && req.method === 'POST')
      .flush(mockData);
  httpTestingController.verify();
  });

任何传递参数数据的解决方案。不知道如何通过测试用例。

【问题讨论】:

    标签: angular karma-jasmine angular8


    【解决方案1】:

    使用HttpTestingController编写服务测试

    • 在您的 TestBed 中导入 HttpClientTestingModule 并使用 TestBed 来 注入HttpTestingController作为变量
    • 这是一个简单的示例:
    let mockHttp: HttpTestingController
    TestBed.configureTestingModule({
      imports: [HttpClientTestingModule],
      ...
    })
    mockHttp = TestBed.get(HttpTestingController);
    
    describe('...', () => {
      it('...', () => {
        yourService.funcWhatToTest().subscribe(
          res => expect(res).toEqual(yourMockData);
        )
        mockHttp.expectOne(req => req.url.includes('yourUrl') && req.method == 'POST' && ...)
          .flush(yourMockData);
        mockHttp.verify();
      }
    })
    
    • 网上有很多资源可用于此测试。搜索关键字HttpTestingController

    【讨论】:

    • 没有describe('...', () =&gt; { it('...', () =&gt; { 部分。你能解释一下吗
    • @NoobCoder 请看这篇文章。这里需要费很大的力气来解释。 medium.com/frontend-fun/…
    • 我添加了编辑。我明白了,但从服务调用函数参数时出错。
    猜你喜欢
    • 1970-01-01
    • 2018-12-11
    • 2020-05-26
    • 2019-04-16
    • 2019-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多