[英]spyOn function not being called Jasmine Angular 2
在我的 Angular 组件的设置中,我有一个功能:
populateForm(id:String, index:number){
let blogs = this.blogsService.returnBlogs()
blogs.map((blog:Blog)=>{
blog._id === id ? this.blogsService.populateForm.next({blog:blog, index:index}) : blog._id = blog._id;
})
}
我已经设置了一个关于按钮点击的测试,它调用 populateForm 函数,该函数调用一个 returnBlogs 函数,来自一个服务,给我一个数组来操作。
但是我在监视 returnBlogs 函数时遇到了麻烦:
it('Should be able to click element and call PopulateForm function with correct parameters', () => {
let blogs = [{_id1: 'xyz', title: 'title1', vidUrl: 'XpiipWULkXk'}, {_id1: 'abc', title: 'title2', vidUrl: 'XpiipWULkXk'}]
blogsService = TestBed.get(BlogsService)
component.blogs = blogsTest
fixture.detectChanges();
let spy = spyOn(blogsService, 'returnBlogs').and.returnValue(blogs);
let domEl = fixture.nativeElement.querySelector('.blog-thumb');
domEl.click();
expect(component.populateForm).toHaveBeenCalledWith('xyz', 1);
expect(spy).toHaveBeenCalled();
});
我收到一个错误:
预期间谍 returnBlogs 已被调用。
任何指针都将不胜感激,我正在努力掌握应该和不应该测试的内容
我已经得到它与以下工作:
it('Should be able to click element and call PopulateForm function with correct parameters', () => {
let blogs = [{_id1: 'xyz', title: 'title1', vidUrl: 'XpiipWULkXk'}, {_id1: 'abc', title: 'title2', vidUrl: 'XpiipWULkXk'}]
blogsService = TestBed.get(BlogsService)
component.blogs = blogsTest
fixture.detectChanges();
let spy = spyOn(blogsService, 'returnBlogs').and.returnValue(blogs);
let domEl = fixture.nativeElement.querySelector('.blog-thumb');
domEl.click();
expect(component.populateForm).toHaveBeenCalledWith('xyz', 1);
blogsService.returnBlogs()
console.log(blogsService.returnBlogs())
expect(blogsService.returnBlogs).toHaveBeenCalled();
});
通过在设置间谍后显式调用blogsService.returnBlogs()
。
记录后,它返回我在脚本顶部设置的博客数组。
但我想:
spyOn(blogsService, 'returnBlogs').and.returnValue(blogs)
调用函数,这是处理这种事情的正确方法吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.