茉莉花:测试一个方法是否被另一个类中的另一个方法调用 [英] Jasmine: Testing whether a method is called by another method from another class
本文介绍了茉莉花:测试一个方法是否被另一个类中的另一个方法调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下2个课程
class B {
public b() { return 1 }
}
class A {
b: B = new B()
public run() { return this.b.b() }
}
我尝试使用以下测试来测试类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()
})
})
我是否误解了茉莉花的测试概念? 我还尝试了"jasmine.createSpy",它也无法正常工作
Did i misunderstood jasmine's testing concept? i also tried `jasmine.createSpy', its also not working
P.S.我确实尝试过手动测试,并确认已经调用了类B的方法b().
P.S. i did tried to test it manually and confirmed that the method b() from class B had been called
推荐答案
b
变量未在任何地方使用,它与a
中的this.b
对象不同,因此未调用b.b
b
variable isn't used anywhere, it isn't same object as this.b
inside a
, so b.b
isn't called.
应该是:
a = new A()
spyOn(a.b, 'b')
a.run()
expect(a.b.b).toHaveBeenCalled()
这篇关于茉莉花:测试一个方法是否被另一个类中的另一个方法调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文