在我的代码中,我在“OK”点击System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.Http.WinHttpException: A connection with the server could not be established
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
at System.Net.Http.WinHttpHandler.<StartRequest>d__105.MoveNext()
--- End of inner exception stack trace ---
at Google.Apis.Http.ConfigurableMessageHandler.<SendAsync>d__43.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
at System.Net.Http.HttpClient.<FinishSendAsync>d__58.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Google.Apis.Requests.ClientServiceRequest`1.<ExecuteUnparsedAsync>d__26.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Google.Apis.Requests.ClientServiceRequest`1.<ExecuteAsync>d__23.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Google.Apis.Requests.ClientServiceRequest`1.<ExecuteAsync>d__22.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at GoogleTranslate.Client.GoogleTranslateClient.<GoogleTranslateAsync>d__3.MoveNext() in C:\myProject\src\GoogleTranslate.Client\GoogleTranslateClient.cs:line 41
提示时触发回调,我想测试是否触发了回调。
在window.confirm
中,我可以通过以下方式存储sinon
函数:
window.confirm
我有没有办法在Jest中实现这种存根?
答案 0 :(得分:30)
在开玩笑中,您可以使用global
覆盖它们。
global.confirm = () => true
正如在每个测试文件在其自己的过程中运行一样,您不必重置设置。
答案 1 :(得分:1)
我刚刚使用了开玩笑,它对我有用:
it("should call my function", () => {
// use mockImplementation if you want to return a value
window.confirm = jest.fn().mockImplementation(() => true)
fireEvent.click(getByText("Supprimer"))
expect(window.confirm).toHaveBeenCalled()
}
答案 2 :(得分:0)
为了消除模拟泄漏到其他测试的机会,我使用了一次模拟:
jest.spyOn(global, 'confirm' as any).mockReturnValueOnce(true);