angular - Jasmine:测试是否使用 HttpTestingController 调用 API
问题描述
我正在为 Angular 应用程序运行单元测试,单击按钮后,API 被调用,我正在测试是否调用了这个 API,并且还使用 HttpTestingController 请求方法!我正在使用 jasmine。
<div class="form-group">
<label class="col-sm-6 col-form-label" for="name">Customer Number</label>
<input type="text" id="accountid" class="form-control col-sm-3" [(ngModel)]="record.accountid" (change)="daemonChanged(0)" name="accountid" required>
<button id="loadbtn" (click)="loadCustomer()" class="col-sm-3 btn">Load</button>
</div>
在我的规格文件中
//tried both the ways to trigger click event it doesn't work
loadButton.triggerEventHandler('click',null);
loadButton.nativeElement.dispatchEvent(new Event('click'));
fixture.detectChanges();
let dummyResponse ={"status":"SUCCESS","data":{}};
let req2 = _HttpTestingController.expectOne('/api/accounts');//fails here
expect(req2.request.method).toBe("POST");
loadCustomer () {
this.http.post('/api/accounts', this.record) //function indeed calls this API!!!
.subscribe(res => {
..some logic...
});
解决方案
推荐阅读
- assembly - 在nasm中乘然后除
- python - dict 到 xlsx 的转换打印不正确
- c# - 当我撞到一个物体时,我的统一重置不起作用
- json - 如何让heroku返回我的错误响应?
- google-cloud-platform - GCP Cloud Logging 中不显示 GKE 应用程序日志(pod/容器/部署)
- node.js - Google App Engine - 间歇性 502 / 对等方重置连接
- javascript - addEventListener 在初始渲染时不起作用
- video - 通过uri从服务器检索React Native expo-av视频,在android上工作但在iOS上返回黑屏
- c++ - 在 C++ 中将函数指针声明为类属性的方法
- javascript - 如何防止 swiper.js 禁用最后一张幻灯片上的“下一步”按钮?