如何使用 mocha Chai 测试 vue 方法
How to test a vue method with mocha Chai
这是摩卡柴单元测试:users.spec.js:
import { expect } from "chai";
import { shallowMount } from "@vue/test-utils";
import Users from "@/components/Users.vue";
const wrapper = shallowMount(Users);
describe("Users test", () => {
it("Displays nice hello message", () => {
expect(wrapper.vm.$data.msg).to.equal("Welcome to Crypto Info");
});
it("users model is an array", () => {
expect(wrapper.vm.$data.users).to.be.an("array");
});
it("getUsers() to be a function", () => {
expect(wrapper.vm.$methods.getUsers()).to.be.a("function");
});
});
我找不到第三次测试的正确语法。我已经尝试了很多东西。 $methods.getUsers() 不工作。
1) Users test
getUsers() to be a function:
TypeError: Cannot read property 'getUsers' of undefined
at Context.it (dist\js\webpack:\tests\unit\users.spec.js:15:1)
你能帮帮我吗?
谢谢。
你能做点像
const result = typeOf wrapper.vm.$methods.getUsers();
然后测试结果是字符串,值为"function"?
expect(result).to.equal("function");
对你有用吗?
该方法将简单地定义为 wrapper.vm
的 属性,因此您可以验证该方法是否存在:
expect(wrapper.vm.getUsers).to.be.a("function")
这是摩卡柴单元测试:users.spec.js:
import { expect } from "chai";
import { shallowMount } from "@vue/test-utils";
import Users from "@/components/Users.vue";
const wrapper = shallowMount(Users);
describe("Users test", () => {
it("Displays nice hello message", () => {
expect(wrapper.vm.$data.msg).to.equal("Welcome to Crypto Info");
});
it("users model is an array", () => {
expect(wrapper.vm.$data.users).to.be.an("array");
});
it("getUsers() to be a function", () => {
expect(wrapper.vm.$methods.getUsers()).to.be.a("function");
});
});
我找不到第三次测试的正确语法。我已经尝试了很多东西。 $methods.getUsers() 不工作。
1) Users test
getUsers() to be a function:
TypeError: Cannot read property 'getUsers' of undefined
at Context.it (dist\js\webpack:\tests\unit\users.spec.js:15:1)
你能帮帮我吗? 谢谢。
你能做点像
const result = typeOf wrapper.vm.$methods.getUsers();
然后测试结果是字符串,值为"function"?
expect(result).to.equal("function");
对你有用吗?
该方法将简单地定义为 wrapper.vm
的 属性,因此您可以验证该方法是否存在:
expect(wrapper.vm.getUsers).to.be.a("function")