Dhr*_*kia 5 unit-testing jasmine typescript jestjs angular
我是在 Angular 中使用 Jest 进行单元测试的初学者。在我的组件中,我有这个this.router.navigate()方法。早些时候我用 Jasmine 来测试它。为此,我做了以下工作:
import { Router } from '@angular/router';
Run Code Online (Sandbox Code Playgroud)
然后,
let router:Router;
Run Code Online (Sandbox Code Playgroud)
然后,在 中beforeEach,
router = TestBed.get(Router);
Run Code Online (Sandbox Code Playgroud)
然后,在测试用例中,
it('should show news intially ', () => {
const navigateSpy = spyOn(router,'navigate');
component.showNews();
expect(navigateSpy).toHaveBeenCalledWith(['/news']);
});
Run Code Online (Sandbox Code Playgroud)
这次测试通过了。但是我如何使用 Jest 来做同样的事情呢?请帮忙。
我有一个ngOnInit()方法调用另一个方法getDynamicMenus()。下面是ngOnInit()方法:
ngOnInit() {
this.getDynamicMenus();
}
getDynamicMenus() {
this.menuService.getAllMenus().subscribe(menus => {
this.menus = menus._embedded.menu;
});
}
Run Code Online (Sandbox Code Playgroud)
请让我知道如何模拟这个方法。在《茉莉花》中,我嘲讽getDynamicMenus()并喊道component.ngOnInit()。但这在 Jest 中不起作用。请帮忙。
与 jasmine 函数等效的spyOn()是jest.spyOn(object, methodName)。
it('should show news intially ', () => {
const navigateSpy = jest.spyOn(router,'navigate');
component.showNews();
expect(navigateSpy).toHaveBeenCalledWith(['/news']);
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17473 次 |
| 最近记录: |