鉴于这个简单的组成部分:
import { Component} from '@angular/core';
@Component({
selector: 'app-component'
})
export class AppComponent {
foo() {
this.bar();
}
bar() {
console.log('hello');
}
}
以下测试如何在我致电bar
foo
describe('AppComponent', () => {
let component: AppComponent;
beforeEach(() => {
component = new AppComponent();
}
it('should foobar', () => {
component.foo();
spyOn(component, 'bar');
expect(component.bar).toHaveBeenCalled();
})
}
我得到了失败的测试:
Expected spy bar to have been called.
答案 0 :(得分:21)
您需要在调用方法之前设置间谍。 Jasmine使用wrap函数来确定它们何时被调用以及它们被调用的内容。在调用spied on方法之前必须先包装该方法以捕获信息。尝试更改您的测试以匹配以下内容:
;with FoodItem as (
select x.Item
from food as f
cross apply (values (Fruit),(Vegetable),(Dairy),(Color)) as x (Item)
where x.Item is not null
and f.Date = @Date
)
select
lt.Item
from ListTable as lt
where case when exists (
select 1
from FoodItem fi
where lt.Item = fi.Item
) then 1 else 0 end = @InclusionFlag
答案 1 :(得分:1)
it("should foobar", () => {
const spy = spyOn(component, "bar");
component.foo();
expect(spy).toHaveBeenCalled();
});