首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用Moxios测试Vue中的API调用

用Moxios测试Vue中的API调用
EN

Stack Overflow用户
提问于 2017-08-01 18:10:20
回答 1查看 2.7K关注 0票数 1

我很难弄清楚如何测试在“挂载”生命周期挂钩中发生的API调用。

我有一个文件组件,负责显示有关“所有者”的一些信息。

这正是我在浏览器中想要/期望的工作方式。

代码语言:javascript
复制
<template>
  <div>
    <h3>Owner Information</h3>
    <table class="table table-striped table-condensed">
      <thead>
        <th>Name</th>
        <th>Address</th>
        <th>Social Security Number</th>
        <th>Ownership Percentage</th>
      </thead>
      <tbody>
        <tr :data-owner-id="owner.id" v-for="owner in owners">
          <td>{{ owner.name }}</td>
          <td>{{ owner.address }}</td>
          <td>{{ owner.censored_ssn }}</td>
          <td>{{ owner.ownership_percentage }}</td>
        </tr>
      </tbody>
    </table>
  </div>
</template>

<script>
  import axios from 'axios';

  export default {
    data() {
      return {
        principal_id: '',
        owners: []
      }
    },
    mounted() {
      const el = document.querySelector('#owner-information');
      this.principal_id = el.dataset.principal;

      var self = this;
      axios.get(`/principals/${this.principal_id}.json`).then(response => {
        response.data.owners.map((owner) => {
          owner.score = '';
          owner.link = '';
          owner.last_pull_date = '';
          self.owners.push(owner);
        });
      });
      .catch(e => {
        console.log(e);
      });
    }
  }
</script>

为了测试,我用的是卡玛,茉莉花和阿芙里亚斯。

下面是一个失败的测试:

代码语言:javascript
复制
import { mount } from 'avoriaz'
import OwnerInformation from '../../app/javascript/packs/OwnerInformation.vue'

describe('OwnerInformation', () => {
  let component

  beforeAll(() => {
    const element = document.createElement('div')
    element.setAttribute('id', 'owner-information')
    element.setAttribute('data-principal', '84033')
    document.body.appendChild(element)

    component = mount(OwnerInformation)
    component.vm.$mount('#owner-information')
  })

  it('retrieves owner information from the API', () => {
    expect(component.data().owners.length).toBe(1)
  })
})

上面的值期望1,但得到0。

因此,现在我想我需要以某种方式对我的API请求进行存根/模拟。一个快速的谷歌搜索将我引向艾斯。所以我用Yarn安装了它,并最终想出了这个。我并不100%确定将moxios.stubRequest放在哪里,但我已经尝试将其放入beforeAll()、beforeEach()和" it“中。

代码语言:javascript
复制
```javascript

从艾灸中进口艾灸

从“avoriaz”导入{挂载}

从'../../app/javascript/packs/OwnerInformation.vue‘导入OwnerInformation

描述(‘OwnerInformation’,() => {

let组件

beforeAll(() ) => {

代码语言:javascript
复制
const element = document.createElement('div')
代码语言:javascript
复制
element.setAttribute('id', 'owner-information')
代码语言:javascript
复制
element.setAttribute('data-principal', '12345')
代码语言:javascript
复制
document.body.appendChild(element)
代码语言:javascript
复制
component = mount(OwnerInformation)
代码语言:javascript
复制
component.vm.$mount('#owner-information')

})

beforeEach(() ) => {

代码语言:javascript
复制
moxios.install()

})

afterEach(() ) => {

代码语言:javascript
复制
moxios.uninstall()

})

它(‘从API检索所有者信息’,() => {

代码语言:javascript
复制
moxios.stubRequest('/principals/12345', {
代码语言:javascript
复制
  status: 200,
代码语言:javascript
复制
  response: {
代码语言:javascript
复制
    id: 1, owners: [
代码语言:javascript
复制
      { name: 'Test Owner', address: '123 Test St.', ssn: '123-12-1234', ownership_percentage: 100 
代码语言:javascript
复制
      }
代码语言:javascript
复制
    ]
代码语言:javascript
复制
  }
代码语言:javascript
复制
})
代码语言:javascript
复制
expect(component.data().owners.length).toBe(1)

})

代码语言:javascript
复制
It appears that the request is not actually be stubbed out.  To troubleshoot, I put a console.log statement just before the axios.get() call (which logs out successfully) and I also put a console.log to log out the response, but this one never shows up which makes me think that the axios request is not working and is not getting "intercepted" by moxios.

```javascript

..。

代码语言:javascript
复制
  console.log('CALLING API...')
代码语言:javascript
复制
  axios.get(`/principals/${this.principal_id}.json`).then(response => {
代码语言:javascript
复制
    console.log('***********************')

..。

代码语言:javascript
复制

当我运行测试时,我确实看到了404,但我不知道为什么:

01 08 2017 12:49:43.483:WARN [web-server]: 404: /principals/12345.json

对我来说,在beforeAll()顶部删除请求是最有意义的,但这也不起作用。

我如何安排这一点,以便艾默斯存根输出API请求,并返回以便我的测试通过?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-08-02 15:33:08

您需要使用moxios.wait()来等待艾莫斯请求的完成:

代码语言:javascript
复制
/*** note the "done" argument -- you must call this method within wait()! ***/
it('retrieves owner information from the API', (done) => {
  moxios.stubRequest('/principals/12345', {
    status: 200,
    response: {
      id: 1, owners: [{ name: 'Test', address: '123 Test St.' }]
    }
  })
  moxios.wait(function() {
    expect(component.data().owners.length).toBe(1)
    done()
  })
})
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45444927

复制
相关文章

相似问题

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