【发布时间】:2018-12-11 06:45:21
【问题描述】:
您好,我一直在尝试为我的注册服务编写测试用例。它有一个 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,但任何帮助都会得到帮助
谢谢
【问题讨论】:
-
如何检查用户名电子邮件密码等的值是否正在提交?
标签: angular typescript karma-jasmine angular-unit-test