Javascript 测试:模拟调度和获取状态
测试应检查是否使用当前用户 ID 12345
调用 inbox.get 来满足 currentuser
过滤条件。
当我运行测试时,无法输入 return(dispatch, getstate)
,我在此处仅添加了两个控制台日志 Console.log(-------- -test 1------------)
正在打印。第二个不是。所以我认为问题在于我如何嘲笑 dispatch
和 getState
。
代码:
function loadInboxSummary(paginationOffset)
Console.log(--------test 1-----------);
{return (dispatch, getState) => {
Console.log(--------testing inside 2 ---------);
const state = getState();
const userId = getCurrentUserId(state);
const filterCriteria = getCurrentFilter(state);
const adjustedInboxViewFilter = {
...filterCriteria,
assignedUserIds: mapCurrentAssignee(userId),
};
return InboxItems.get(
userId,
adjustedInboxViewFilter,
paginationOffset,
) ................
测试调用 InboxItems.get(..) 时,它具有 adjustmentInboxView 的正确参数。
测试:
describe("loadInboxSummary", () => {
const dispatch = jest.fn();
const getState = jest.fn().mockReturnValue("mock_state");
beforeEach(() => {
jest.spyOn(getCurrentUserDetails, "getCurrentUserId").mockReturnValue("12345");
jest.spyOn(getCurrentFilter, "default").mockReturnValue({assignedUserIds: ["currentUser"]});
jest.spyOn(InboxItems, "get").mockResolvedValue({});
});
it("calls InboxItems.get with current user id", () => {
loadInboxSummary("mock_pagination")(dispatch, getState);
const adjustedFilters = {
assignedUserIds: ["12345"],
};
expect(InboxItems.get).toHaveBeenCalledWith(
"12345",
adjustedFilters,
"mock_pagination",
); }); });
Error: expect(jest.fn()).toHaveBeenCalledWith(...expected)
Expected: "12345", {"assignedUserIds": ["12345"]}, "mock_pagination"
Number of calls: 0
这是我第一次进行前端测试,我搜索了堆栈溢出以寻找可能的解决方案,但它们看起来完全不同/具有异步函数,所以我无法理解它们。非常感谢一些帮助。不确定是什么原因导致此错误,因此请随时提出问题。
The test should be checking if inbox.get is getting called with current user id 12345
for currentuser
filter criteria.
When I run the tests, it's not able to enter return(dispatch, getstate)
, out of the two console logs that i added here only Console.log(--------test 1-----------)
is being printed. Second one is not. So im thinking that the problem is with how im mocking dispatch
and getState
.
Code:
function loadInboxSummary(paginationOffset)
Console.log(--------test 1-----------);
{return (dispatch, getState) => {
Console.log(--------testing inside 2 ---------);
const state = getState();
const userId = getCurrentUserId(state);
const filterCriteria = getCurrentFilter(state);
const adjustedInboxViewFilter = {
...filterCriteria,
assignedUserIds: mapCurrentAssignee(userId),
};
return InboxItems.get(
userId,
adjustedInboxViewFilter,
paginationOffset,
) ................
Testing when InboxItems.get(..) is called, it has correct parameters for adjustedInboxView.
Test:
describe("loadInboxSummary", () => {
const dispatch = jest.fn();
const getState = jest.fn().mockReturnValue("mock_state");
beforeEach(() => {
jest.spyOn(getCurrentUserDetails, "getCurrentUserId").mockReturnValue("12345");
jest.spyOn(getCurrentFilter, "default").mockReturnValue({assignedUserIds: ["currentUser"]});
jest.spyOn(InboxItems, "get").mockResolvedValue({});
});
it("calls InboxItems.get with current user id", () => {
loadInboxSummary("mock_pagination")(dispatch, getState);
const adjustedFilters = {
assignedUserIds: ["12345"],
};
expect(InboxItems.get).toHaveBeenCalledWith(
"12345",
adjustedFilters,
"mock_pagination",
); }); });
Error: expect(jest.fn()).toHaveBeenCalledWith(...expected)
Expected: "12345", {"assignedUserIds": ["12345"]}, "mock_pagination"
Number of calls: 0
This is my first time doing front end testing, I searched stack overflow for possible solutions but they looked quite different/had async functions so I couldn't understand them. Would really appreciate some help. Not sure what could be causing this error so feel free to ask questions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论