我有以下假设情景:
instance.myFunc
我需要使用Jest创建一个测试,确保调用(define play-asynchronously #f)
(define sound-test #t)
(define (sound-test!)
(if sound-test
#f
#t))
(define (in-game-loop)
(if sound-test
(if (play-sound "flight_of_the_bumblebee.mp3" play-asynchronously)
;Geschreven in 1899-1900 door Nikolaj Rimski-Korsakov
;Gedownload op 16/05/2018 van https://ia802606.us.archive.org/19/items/FlightOfTheBumblebee/flight_of_the_bumblebee_2.mp3
(in-game-loop)
'done)
(begin
(in-menu-loop)
(set! sound-test #t))))
(define (in-menu-loop)
(if sound-test
(if (play-sound "Gymnopedie-No-1.mp3" play-asynchronously)
;Geschreven in 1888 door Erik Satie
;Gedownload op 16/05/018 van https://incompetech.com/music/royalty-free/?keywords=gymnopedie
(in-menu-loop)
'done)
(begin
(in-game-loop)
(set! sound-test #t))))
答案 0 :(得分:1)
其中一个选项是用模拟实现替换MyClass模块
it("Test myfunc called in functionToBeTested", () => {
functionToBeTested()
expect(mockmyfunc).toHaveBeenCalled()
})
然后写下面的测试
export class MyClass {
myFunc() {
// do smth
}
}
请注意,这不是唯一的方法,您可以深入了解https://facebook.github.io/jest/docs/en/es6-class-mocks.html以寻找其他选择。
如果myfunc是一个实际的函数(我猜它不是一个选项,因为它是外部包?)
import MyClass from "path/to/external/package/MyClass"
jest.mock("path/to/external/package/MyClass")
it("Test myfunc called in functionToBeTested", () => {
functionToBeTested()
const mockMyFunc = MyClass.mock.instances[0].myFunc
expect(mockMyFunc).toHaveBeenCalled()
})
你不需要替换实现,你可以使用jest的automock
function checkForMatch() {
if ( $(openedCardsList[0]).is(openedCardsList[1]) ) {
$(openedCardsList[0]).removeClass('open show').addClass('match');
$(openedCardsList[1]).toggleClass('open show');
$(openedCardsList[1]).toggleClass('match');
$(openedCardsList) = [];
}
else {unFlip()}
}
答案 1 :(得分:0)
您可以模拟该类并将该文件的默认导出分配给变量,如下所示:
jest.mock('../../utils/api/api');
const FakeClass = require('../someFile.js').default;
然后访问模拟类上的函数调用,如下所示:
FakeClass.prototype.myFunc.mock.calls