Javascript 有我必须为之编写代码的预测试。可以让代码工作,但无法通过测试
我还是个新手。三个月学习javascript。因此,在写入方向上进行任何推送都会有帮助 所以我现在正在做一项作业,这是我们第一次做测试。测试是预先为我们准备的,我们只需要编写代码并通过测试。我在通过测试时遇到了问题,即使我的代码正在执行测试所检查的内容Javascript 有我必须为之编写代码的预测试。可以让代码工作,但无法通过测试,javascript,node.js,class,Javascript,Node.js,Class,我还是个新手。三个月学习javascript。因此,在写入方向上进行任何推送都会有帮助 所以我现在正在做一项作业,这是我们第一次做测试。测试是预先为我们准备的,我们只需要编写代码并通过测试。我在通过测试时遇到了问题,即使我的代码正在执行测试所检查的内容 const inquirer = require('inquirer'); class Employee { constructor(name, id, email) { this.name = name;
const inquirer = require('inquirer');
class Employee {
constructor(name, id, email) {
this.name = name;
this.id = id;
this.email = email;
}
getName() {
return inquirer.prompt([
{
type: 'input',
name: 'name',
message: "What is the employee's name?"
}
]).then(answers => {
this.name = answers.name;
this.getId();
})
}
getId() {
return inquirer.prompt([
{
type: 'number',
name: 'id',
message: "What is the employee's ID?"
}
]).then(answers => {
this.id = answers.id;
this.getEmail();
})
}
getEmail(){
return inquirer.prompt([
{
type: 'input',
name: 'email',
message: "What is the employee's email?"
}
]).then(answers => {
this.email = answers.email;
this.getRole();
})
}
getRole(){
this.role = 'employee'
console.log(this)
}
}
const e = new Employee();
e.getName();
测试是这样的
const Employee = require("../lib/Employee");
test("Can instantiate Employee instance", () => {
const e = new Employee();
expect(typeof(e)).toBe("object");
});
test("Can set name via constructor arguments", () => {
const name = "Alice";
const e = new Employee(name);
expect(e.name).toBe(name);
});
test("Can set id via constructor argument", () => {
const testValue = 100;
const e = new Employee("Foo", testValue);
expect(e.id).toBe(testValue);
});
test("Can set email via constructor argument", () => {
const testValue = "test@test.com";
const e = new Employee("Foo", 1, testValue);
expect(e.email).toBe(testValue);
});
test("Can get name via getName()", () => {
const testValue = "Alice";
const e = new Employee(testValue);
expect(e.getName()).toBe(testValue);
});
test("Can get id via getId()", () => {
const testValue = 100;
const e = new Employee("Foo", testValue);
expect(e.getId()).toBe(testValue);
});
test("Can get email via getEmail()", () => {
const testValue = "test@test.com";
const e = new Employee("Foo", 1, testValue);
expect(e.getEmail()).toBe(testValue);
});
test("getRole() should return \"Employee\"", () => {
const testValue = "Employee";
const e = new Employee("Alice", 1, "test@test.com");
expect(e.getRole()).toBe(testValue);
});
当我运行代码时,它会创建一个包含所有问题答案的对象,但测试只会返回失败的结果,因为雇员不是构造函数
有人给了我一些提示或方法来引导我走错方向吗?我不知道你是否明白,但我也有同样的问题。问题是您必须添加module.exports=Employee; 否则不能在其他文件中使用。 希望这有帮助