嘲笑功能没有被开玩笑

时间:2019-02-14 13:26:25

标签: javascript reactjs unit-testing jestjs enzyme

我正在测试:

with()

通过写作:

WHERE EXISTS

然后返回$doctors = Doctor::with('categories')->whereHas('categories', function ($query) { $query->where('categories.id', 1); })->get(); ,谈论onChange(value) { this.setState({ postcode: value }); if (value.length >= 3) this.listSuggestions(value); } 。这是为什么?如果删除test("onChange updates the state", () => { const postalCodeWrapper = mount(<PostalCode />); const instance = postalCodeWrapper.instance(); const listSuggestions = jest.fn(instance.listSuggestions); const mockValue = "this is mock value"; instance.onChange(mockValue); expect(instance.state.postcode).toBe(mockValue); expect(listSuggestions).toHaveBeenCalled(); }); 语句,也会发生同样的事情。

1 个答案:

答案 0 :(得分:2)

您的问题是这一行:

const listSuggestions = jest.fn(instance.listSuggestions);

调用该函数时,jest.fn返回一个 new 函数,而不是对现有方法进行变异。您将其分配给const,但是您的组件将调用原始文件。

将其更改为:

instance.listSuggestions = jest.fn(instance.listSuggestions);

要使用模拟功能覆盖组件方法,然后在条件检查通过的情况下调用该方法。