素材牛VIP会员
vue单元测试 如何覆盖promise对象里面的代码
 鬼***5  分类:JavaScript  人气:736  回帖:1  发布于6年前 收藏
.vue
login(){
  apiUser.login(this.phone, this.password).then((res) => {
    const data = res.data
    if (data.code === 200) {
      this.$store.commit('login')
      this.$store.commit('modifyUserId', data.profile.userId)
      this.storage(data.profile.userId, data.profile.nickname, data.profile.avatarUrl)
      alert('登录成功!')
      this.close()
    } else {
      alert(res.data.msg)
    }
  })
}

.spec.js
it('输入错误的手机号或密码', () => {
  vm.phone = '123123123'
  vm.password = '12312'
  vm.login()
  expect()
})

例如这段代码, 在test中调用login()后,箭头函数里面的代码没有被覆盖掉,请问在单测中如何写呢??

讨论这个帖子(1)垃圾回帖将一律封号处理……

Lv3 码奴
逆***动 PHP开发工程师 6年前#1
 文明上网,理性发言!   😉 阿里云幸运券,戳我领取