1

嗨,我一直在尝试为我的注册服务编写测试用例。它有一个 url,它发送 4 个值。服务如下:

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http'



interface registerResponse {
  success: boolean,
  message: string
}

@Injectable()
export class AuthService {

private _regurl ="http://localhost:3000/api/register"
  constructor(private http: HttpClient) { }



  registerUser(username,email,date,  password) {
   
    return this.http.post<registerResponse>(this._regurl, {
      username,
      email,
      date,
      password
      
     
    })
  }

}

我仍在学习如何编写测试用例,我试图让 registerResponse 工作。

这是我到目前为止所写的

import { TestBed, async, inject } from '@angular/core/testing';
import {
  HttpModule,
  Http,
  Response,
  ResponseOptions,
  XHRBackend
} from '@angular/http';
import { MockBackend } from '@angular/http/testing';
import { AuthService } from './auth.service';
import { HttpClient } from '@angular/common/http'


describe('RegisterService', () => {

  beforeEach(() => {

    TestBed.configureTestingModule({
      imports: [HttpModule],
      providers: [
        { provide: HttpClient, useValue: 'http://localhost:3000/api/register' },
        AuthService,
        { provide: XHRBackend, useClass: MockBackend },
      ]
    });
  });

我知道这并不多,我只定义了 mockbackend,但任何帮助都会得到帮助

谢谢你

4

1 回答 1

2

要开始测试对 的调用HttpClient,请导入HttpClientTestingModule和模拟控制器,HttpTestingController以及您的测试所需的其他符号。

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

然后添加并HttpClientTestingModule继续TestBed设置被测服务。

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

describe('RegisterService', () => {

  let service: AuthService;
  let httpMock: HttpTestingController;

  beforeEach(() => {

    TestBed.configureTestingModule({
      imports: [HttpClientTestingModule],
      providers: [AuthService]
    });

    service = TestBed.get(AuthService);
    httpMock = TestBed.get(HttpTestingController);
  });

  afterEach(() => {
    httpMock.verify();
  });

  describe('#registerUser tests', () => {
    it('Should return registerResponse by making a POST request to the given url', () => {

      const username = 'username';
      const email = 'email';
      const date = 'date';
      const password = 'password';

      const mockResponse: RegisterResponse = {
        success: true,
        message: 'Successfull'
      };
      service.registerUser(username, email, date, password).subscribe((res) => {
        expect(res).toBe(mockResponse);
      });

      const req = httpMock.expectOne('http://localhost:3000/api/register');
      expect(req.request.method).toBe('POST');
      req.flush(mockResponse);
    });
  });
});

现在,在您的测试中发出的请求将到达测试后端,而不是普通的后端

此设置还调用TestBed.get()注入模拟控制器,以便在测试期间可以引用它。

以下expectOne()将匹配请求的 URL。如果没有请求或多个请求匹配该 URLexpectOne()将抛出。

const req = httpMock.expectOne('http://localhost:3000/api/register');

在官方 Angular 文档中阅读有关测试使用HttpClient和测试 Http 请求的 Angular服务的更多信息。

为您服务。导出界面。

export interface RegisterResponse {
  success: boolean,
  message: string
}

但我建议您将数据模型移动到不同的目录或文件中。

然后像这样将接口导入到tes文件中。

import { RegisterResponse } from 'path-to-interface';
于 2018-07-03T04:11:24.853 回答