0
功能的功能:玩笑單元測試調用如果我有代碼這樣的事情,它返回一個承諾
import externalThing from 'externalThing'
import anotherThing from 'anotherThing'
function functionIWantToTest()
{
externalThing.doSomething()
.then(data=>{
anotherThing.do(data.Id)
})
}
什麼是最好的實踐方式,這個單元測試?
理想的測試將是這樣的:
import externalThing from 'externalThing'
import anotherThing from 'anotherThing'
jest.mock('externalThing',()=>jest.fn())
jest.mock('anotherThing',()=>jest.fn())
describe('when calling functionIWantToTest',()=>{
beforeEach(()=>{
anotherThing.do=jest.fn()
//mock external thing somehow so that it still the 'then' code
functionIWantToTest()
})
it('anotherThing should be called',()=>{
expect(anotherThing.do).toHaveBeenCalledWith(1)
});
});
但是當我嘗試我剛剛結束了建築嘲笑功能鏈jest.fn(),並沒有實際的代碼被執行。
什麼是預期的結果'functionIWantToTest()'調用? – guest271314
嗨@ guest271314它在測試中:),用於'anotherThing.do'已用param調用。 – Dan
還沒有嘗試'笑話'。 'jest.fn()'返回什麼? 「anotherThing.do」有兩種不同的賦值方式嗎?注意,沒有實際返回的值來自'functionIWantToTest()'調用 – guest271314