使用本机和测试库测试函数调用时出现的问题

问题描述

我有一个登录表单,它一旦提交就会调用一个从钩子返回的函数。为了让我的测试更容易,我收到了我的钩子作为道具,它的认值是导入的钩子。但在测试中使用模拟钩子和我想测试的方法

我的测试看起来像这样

it('Should correctly send the form data',() => {
            const mockSignIn = jest.fn();

            const authHookMock = () => ({
                signIn: (email: string,password: string) => {
                    mockSignIn(email,password)
                },resetPassword: (email: string) => { },isLoggedIn: () => false
            })

            const { getByText,getByPlaceholderText } = render(<Auth useAuthHook={authHookMock} />);

            const emailInput = getByPlaceholderText("Ex: nome@exemplo.com")
            const passWordInput = getByPlaceholderText("Ex: 123_teste")
            const actionButon = getByText('Entrar')

            const mockEmail = 'teste@gmail.com';
            const mockPassword = '134343';

            fireEvent.change(emailInput,mockEmail);
            fireEvent.change(passWordInput,mockPassword);
            fireEvent.click(actionButon);

            expect(mockSignIn).toHaveBeenCalledWith(mockEmail,mockPassword);
        })

预期结果:

  • 我希望我的测试通过,并使用传递的值调用我的函数

实际结果:


    Auth route tests › Login › Should correctly send the form data

    expect(jest.fn()).toHaveBeenCalledWith(...expected)

    Expected: "teste@gmail.com","134343"

    Number of calls: 0

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...