为了账号安全,请及时绑定邮箱和手机立即绑定

如何解决sinonJS存根问题?

如何解决sinonJS存根问题?

吃鸡游戏 2023-09-28 17:33:52
所以我试图用SinonJS 存根请求。在每次测试之前,它应该使用已解决的虚假信息来模拟请求,但它似乎没有按预期工作。尝试使用 解决Promise.resolve,但它也无法按我的预期工作。这是测试代码:describe("Store | Users actions", () => {  let commit = null;  let page = 1;  let itemsPerPage = 2;  const users_response = {    status: 200,    data: [{      "id": 1,      "name": "Leanne Graham",      "username": "Bret",      "email": "Sincere@april.biz"    },    {      "id": 2,      "name": "Ervin Howell",      "username": "Antonette",      "email": "Shanna@melissa.tv"    }]  };  beforeEach(() => {    commit = sinon.spy();    sinon      .stub(api.users, "list").resolves();  });  afterEach(() => {    api.users.list.restore();  });  it("should list users", () => {    users.actions.list({ commit }, { page, itemsPerPage });    expect(commit).to.have.been.calledWith("UNSET_ERROR");    expect(commit).to.have.been.calledWith("GET_PAGINATED", users_response);  });});这是我收到的错误:  1) Store | Users actions       should list users:     AssertionError: expected spy to have been called with arguments GET_PAGINATED, {  data: [{ email: "Sincere@april.biz", id: 1, name: "Leanne Graham", username: "Bret" }, { email: "Shanna@melissa.tv", id: 2, name: "Ervin Howell", username: "Antonette" }],  status: 200}"UNSET_ERROR" "GET_PAGINATED"{  data: [{ email: "Sincere@april.biz", id: 1, name: "Leanne Graham", username: "Bret" }, { email: "Shanna@melissa.tv", id: 2, name: "Ervin Howell", username: "Antonette" }],  status: 200}      at Context.<anonymous> (dist/js/webpack:/tests/unit/store/users.spec.js:184:1)list({ commit }, { page, itemsPerPage, sort, search }) {      commit("UNSET_ERROR");      return api.users        .list(page, itemsPerPage, sort, search)        .then((users) => commit("GET_PAGINATED", users.data))        .catch((error) => commit("SET_ERROR", error));    }我在这里做错了什么?任何帮助深表感谢。
查看完整描述

1 回答

?
holdtom

TA贡献1805条经验 获得超10个赞

这是因为你的第二个提交函数调用是在 Promise then 方法内部。


您需要等待 users.actions.list()。


例如:


  beforeEach(() => {

    commit = sinon.spy();

    // Note: add users_response here.

    sinon.stub(api.users, "list").resolves(users_response);

  });


  // Use async here.

  it("should list users", async () => {

    // Use await here.

    await users.actions.list({ commit }, { page, itemsPerPage });

    expect(commit).to.have.been.calledWith("UNSET_ERROR");

    // Note: expect with property data, because called with: users.data.

    expect(commit).to.have.been.calledWith("GET_PAGINATED", users_response.data);

  });


查看完整回答
反对 回复 2023-09-28
  • 1 回答
  • 0 关注
  • 46 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信