首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >`moxios.wait`从不在使用Jest、Vue testing和Moxios测试VueJS时执行

`moxios.wait`从不在使用Jest、Vue testing和Moxios测试VueJS时执行
EN

Stack Overflow用户
提问于 2018-03-06 12:13:19
回答 1查看 2.6K关注 0票数 4

我试图为VueJS组件编写第一个单元测试,该组件在呈现时从API端点获取一些数据:

我的VueJS组件:

代码语言:javascript
复制
import axios from 'axios';

export default {
  props: {
          userIndexUrl: {
            required: true
          }
        },
  data() {
    userList: [],
    tableIsLoading: true
  },
  created() {
    const url = this.userIndexUrl;
    axios.get(url)
    .then(response => {
      this.userList = response.data.data;
      this.tableIsLoading = false;
    })
  },
}

我的测试是:

代码语言:javascript
复制
import { mount } from 'vue-test-utils'
import moxios from 'moxios'
import UsersAddRemove from 'users_add_remove.vue'

describe('UsersAddRemove', () => {
  const PROPS_DATA = {
      userIndexUrl: '/users.json'
  }

  beforeEach(() => {
    moxios.install();
    moxios.stubRequest('/users1.json', {
      status: 200,
      response: {
        "count": 1,
        "data": [
            { "id" : 1,
              "email" : "brayan@schaeferkshlerin.net",
              "first_name" : "Kenneth",
              "last_name" : "Connelly"
            }
          ]
        }
    });
  });

  afterEach(() => {
    moxios.uninstall();
  });

  it('loads users from remote JSON endpoint and renders table', () => {
    const wrapper = mount(UsersAddRemove, { propsData: PROPS_DATA });
    moxios.wait(() => {
      expect(wrapper.html()).toContain('<td class="">brayan@schaeferkshlerin1.net</td>');
      done();
    });
  });
});

因此,我希望实现的是这个组件被实例化,然后它执行一个API调用,这个调用被Moxios捕获,然后它应该执行moxios.wait,这是没有发生的。这些测试似乎忽略了moxios.wait,即使它不应该通过,它也会成功通过。

我在这里错过了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-03-06 12:15:33

尝试添加done作为参数:

代码语言:javascript
复制
  it('loads users from remote JSON endpoint and renders table', (done) => {
  // -----------------------------------------------------------^^^^^^
    const wrapper = mount(UsersAddRemove, { propsData: PROPS_DATA });
    moxios.wait(() => {
      expect(wrapper.html()).toContain('<td class="">brayan@schaeferkshlerin1.net</td>');
      done();
    });
  });

等待Ajax

变动如下:

代码语言:javascript
复制
// remove the stubRequest of beforeEach()
beforeEach(() => {
  moxios.install();
});

// afterEach stays the same

it('loads users from remote JSON endpoint and renders table', (done) => {
  const wrapper = mount(UsersAddRemove, { propsData: PROPS_DATA });

  moxios.wait(() => {
    let request = moxios.requests.mostRecent();
    request.respondWith({
      status: 200,
      response: {
        "count": 1,
        "data": [{
          "id": 1,
          "email": "brayan@schaeferkshlerin.net",
          "first_name": "Kenneth",
          "last_name": "Connelly"
        }]
      }
    }).then(function() {
      expect(wrapper.html()).toContain('<td class="">brayan@schaeferkshlerin1.net</td>');
      done();
    });
  });
});
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49130579

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档