问题
I have 2 classes as below
class B {
public b() { return 1 }
}
class A {
b: B = new B()
public run() { return this.b.b() }
}
I tried to use the following test to test did method b() from class B, but the test is not working
describe('A spy', () => {
let a: A
let b: B
beforeEach(() => {
a = new A()
b = new B()
spyOn(b, 'b')
a.run()
})
it('tracks that the spy was called', () => {
expect(b.b).toHaveBeenCalled()
})
})
Did i misunderstood jasmine's testing concept? i also tried `jasmine.createSpy', its also not working
P.S. i did tried to test it manually and confirmed that the method b() from class B had been called
回答1:
b
variable isn't used anywhere, it isn't same object as this.b
inside a
, so b.b
isn't called.
It should be:
a = new A()
spyOn(a.b, 'b')
a.run()
expect(a.b.b).toHaveBeenCalled()
来源:https://stackoverflow.com/questions/54068845/jasmine-testing-whether-a-method-is-called-by-another-method-from-another-class