【发布时间】:2021-03-21 20:26:24
【问题描述】:
我有 fetchBooks 方法从后端检索数据,然后填充 books 变量。
import { Component, OnInit } from "@angular/core";
@Component({
selector: "app-root",
templateUrl: "./app.component.html",
})
export class AppComponent implements OnInit {
isLoading: boolean;
books;
constructor(private booksService: BooksService) {}
ngOnInit() {
this.fetchBooks();
}
fetchBooks(): void {
this.isLoading = true;
this.booksService
.fetchBooks()
.subscribe(
response => {
this.isLoading = false;
this.books = response.data;
// ..
},
error => {
this.isLoading = false;
// ..
}
);
}
}
我需要为isLoading 标志编写单元测试。我写了这样的东西
it('should work', async () => {
sut.fetchBooks();
// ...
expect(sut.isLoading).toBe(true);
// ...
expect(sut.isLoading).toBe(false);
});
但我与其他人斗争。也许有人知道如何解决它或知道一些解释它的文章?
【问题讨论】:
-
fetchBooks方法是组件方法吗?请提供完整、最少的代码。 -
是的,它是组件方法!我已经更新了示例
-
@bubbles 感谢分享这篇文章,但我不认为它解释了如何测试挂起状态。
标签: angular typescript rxjs jasmine angular-test