在单元测试中模拟 vue-router 的 push() 方法 - Vue 3
我是 Vue 和 StackOverflow 的新手,但我会尝试解释我的问题。
当我尝试为我的应用程序运行单元测试时,出现类型错误。
“TypeError:无法读取未定义的属性(读取‘push’)”
https://i.sstatic.net /pqUkZ.png
程序完全正常运行,否则只是测试失败了。
测试尝试在应用程序中运行 $router.push,但不能,因为它不知道那是什么,如何在测试时成功模拟 Push 方法?
我的 example.spec.js 测试代码如下所示:
import { shallowMount } from '@vue/test-utils'
import LoginView from '@/views/LoginView.vue'
describe('LoginView.vue', () => {
it('loginSuccess-test', async () => {
const wrapper = shallowMount(LoginView, {
mocks: {
$router: {
push: () => {}
}
}
})
await wrapper.setData({username:'user', password:'pass'})
await wrapper.find('button').trigger('click')
const statusId = wrapper.find('#loginstatusLabel');
expect(wrapper.find('#loginstatusLabel').element.textContent).toBe('true')
})
})
我的 LoginView.vue 方法代码如下所示:
methods: {
checkCredentials() {
if (this.username === 'user' && this.password === 'pass') {
this.loginSuccess = 'true';
this.$router.push({ name: 'home' });
}
else {
this.toast.error("Username or password is incorrect Try again");
this.message = 'Login failed!';
this.isActive = false;
}
}
</script>
我试图使 Push 方法在用户成功登录时将用户重定向到“home”并在测试中模拟重定向。
任何帮助将不胜感激
I'm new to Vue and StackOverflow, but I'll try to explain my problem.
I get a TypeError when I try to run the unit test for my application.
"TypeError: Cannot read properties of undefined (reading 'push')"
https://i.sstatic.net/pqUkZ.png
The program runs exactly as it should otherwise, it's only the test that fails.
The test tries to run $router.push in the application, but can't, because it doesn't know what that is, how can I successfully mock the push method while testing?
My example.spec.js test code looks like this:
import { shallowMount } from '@vue/test-utils'
import LoginView from '@/views/LoginView.vue'
describe('LoginView.vue', () => {
it('loginSuccess-test', async () => {
const wrapper = shallowMount(LoginView, {
mocks: {
$router: {
push: () => {}
}
}
})
await wrapper.setData({username:'user', password:'pass'})
await wrapper.find('button').trigger('click')
const statusId = wrapper.find('#loginstatusLabel');
expect(wrapper.find('#loginstatusLabel').element.textContent).toBe('true')
})
})
My LoginView.vue methods code looks like this:
methods: {
checkCredentials() {
if (this.username === 'user' && this.password === 'pass') {
this.loginSuccess = 'true';
this.$router.push({ name: 'home' });
}
else {
this.toast.error("Username or password is incorrect Try again");
this.message = 'Login failed!';
this.isActive = false;
}
}
</script>
I'm trying to make the push method redirect the user to "home" when he successfully logs in and mock the redirection in testing.
Any help will be greatly appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 vue3 中,您需要将
mocks
属性包装在global
属性中Vue2
Vue3
来源:https://test-utils.vuejs.org/guide/advanced/vue-router.html
In vue3 you need to wrap the
mocks
property insideglobal
propertyVue2
Vue3
Source: https://test-utils.vuejs.org/guide/advanced/vue-router.html
试试这个:
Try this: