Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/407.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何解决sinonJS存根?_Javascript_Unit Testing_Vue.js_Vuex_Sinon - Fatal编程技术网

Javascript 如何解决sinonJS存根?

Javascript 如何解决sinonJS存根?,javascript,unit-testing,vue.js,vuex,sinon,Javascript,Unit Testing,Vue.js,Vuex,Sinon,所以我正试图用SinonJS截取一个请求。 在每次测试之前,它都应该使用已解析的假信息模拟请求,但它似乎没有按预期工作。尝试使用Promise.resolve解决问题,但也没有达到预期效果 以下是测试代码: describe("Store | Users actions", () => { let commit = null; let page = 1; let itemsPerPage = 2; const users_response = {

所以我正试图用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)
我做错了什么?非常感谢您的帮助


编辑:添加了列表函数

,这是因为您的第二个提交函数调用在Promise-then方法中

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

例如:

beforeach(()=>{
commit=sinon.spy();
//注意:在此处添加用户\u响应。
sinon.stub(api.users,“列表”).解析(用户响应);
});
//在这里使用异步。
它(“应该列出用户”,异步()=>{
//使用此处等待。
wait users.actions.list({commit},{page,itemsPerPage});
expect(commit).to.have.been.calledWith(“UNSET_ERROR”);
//注意:使用属性数据进行expect,因为使用:users.data调用。
expect(commit).to.have.been.calledWith(“GET_PAGINATED”,users_response.data);
});

请提供要测试的代码。您的错误消息意味着:您的第二个预期失败,因为:(可能)您的spy(commit)未使用参数“get_PAGINATED”(作为第一个参数)和用户响应(作为第二个参数)调用。您的间谍只被调用了一次,带有1个arg“UNSET_ERROR”。没有问题,您的单元测试工作正常,因为您没有提供测试中的代码和上下文。我添加了我正在尝试测试的函数,可能值得放入一个断言来检查是否未调用
commit('SET_ERROR')
。我添加了一个断言来检查是否未调用它。
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));
    }