gpt4 book ai didi

javascript - 在单元测试中接收语法错误

转载 作者:行者123 更新时间:2023-12-03 08:24:45 25 4
gpt4 key购买 nike

我已经更新了这篇文章;请参阅粗体文本后面的编辑。
我正在学习 javascript 并且正在练习单元测试。我完成的最新测试不断返回语法错误,尽管我已经 (1) 通过将模块的工作输出复制并粘贴到单元测试中来验证预期结果的准确性,并且 (2) 确认每个括号、括号和花括号支架适本地配对。我尝试根据另一个 StackOverflow 问题中给出的答案在 ATOM 文本编辑器中检查不可见字符,但没有立即看到任何字符。
错误信息:

SyntaxError: C:/Users/elber/.atom/packages/script/node_modules/.bin/babel: Unexpected token, expected , (2380:16)
2378 | assert.strictEqual(result.commands,
2379 | [
> 2380 | Command { commandType: 'MODE_CHANGE', value: 'LOW_POWER' },
| ^
2381 | Command { commandType: 'STATUS_CHECK', value: undefined }
2382 | ]
复制并粘贴到单元测试中的正确输出:
[
Command { commandType: 'MODE_CHANGE', value: 'LOW_POWER' },
Command { commandType: 'STATUS_CHECK', value: undefined }
]
模块和相关单元测试:
// ---------------- Command module ---------------

class Command {
constructor(commandType, value) {
this.commandType = commandType;
if (!commandType) {
throw Error("Command type required.");
}
this.value = value;
}

}

// ---------------- Message module ---------------

class Message {
constructor(name, commands) {
this.name = name;
if (!name) {
throw Error("Name required.");
}
this.commands = commands;
}

}

// ---------------- running modules for correct output ---------------

let commands = [new Command('MODE_CHANGE', 'LOW_POWER'), new Command('STATUS_CHECK')];
let message = new Message('Test message with two commands', commands);

console.log (message.commands);

// ---------------- Unit Test ---------------

describe("Message class", function() {

it("contains a commands array passed into the constructor as 2nd argument", function() {
let result = new Message ("Test message with two commands", [new Command('MODE_CHANGE', 'LOW_POWER'), new Command('STATUS_CHECK')]);

assert.strictEqual(result.commands,
[
Command { commandType: 'MODE_CHANGE', value: 'LOW_POWER' },
Command { commandType: 'STATUS_CHECK', value: undefined }
]
);
});
});
我还尝试在不使用类名的情况下更新代码(因此将答案中建议的以下代码复制并粘贴到下面的单元测试描述中):
describe("Message class", function() {

it("contains a commands array passed into the constructor as 2nd argument", function() {
let result = new Message ("Test message with two commands", [new Command('MODE_CHANGE', 'LOW_POWER'), new Command('STATUS_CHECK')]);

assert.strictEqual(result.commands,
[
{ commandType: 'MODE_CHANGE', value: 'LOW_POWER' },
{ commandType: 'STATUS_CHECK', value: undefined }
]
);
});
});
在 Repl.it 中:通过该编辑,在 Repl.it 中生成的错误消息如下所示:
Failures:
1) Message class contains a commands array passed into the constructor as 2nd argument
Message:
AssertionError: Expected "actual" to be reference-equal to "expected":
+ actual - expected

[
+ Command {
- {
commandType: 'MODE_CHANGE',
value: 'LOW_POWER'
},
+ Command {
- {
commandType: 'STATUS_CHECK',
value: undefined
}
]
Stack:
error properties: Object({ generatedMessage: true, code: 'ERR_ASSERTION', actual: [ Command({ commandType: 'MODE_CHANGE', value: 'LOW_POWER' }), Command({ commandType: 'STATUS_CHECK', value: undefined }) ], expected: [ Object({ commandType: 'MODE_CHANGE', value: 'LOW_POWER' }), Object({ commandType: 'STATUS_CHECK', value: undefined }) ], operator: 'strictEqual' })
at <Jasmine>
at UserContext.<anonymous> (/home/runner/mars-rover-starter-1/spec/message.spec.js:34:12)
at <Jasmine>
at processImmediate (internal/timers.js:456:21)
at process.topLevelDomainCallback (domain.js:137:15)
在 ATOM 中,ATOM 中生成的错误消息如下所示:
[stdin]:2377
describe("Message class", function () {
^

ReferenceError: describe is not defined
at [stdin]:2377:1
at Script.runInThisContext (vm.js:120:20)
at Object.runInThisContext (vm.js:311:38)
at Object.<anonymous> ([stdin]-wrapper:10:26)
at Module._compile (internal/modules/cjs/loader.js:1138:30)
at evalScript (internal/process/execution.js:94:25)
at internal/main/eval_stdin.js:29:5
at Socket.<anonymous> (internal/process/execution.js:207:5)
at Socket.emit (events.js:327:22)
at endReadableNT (_stream_readable.js:1221:12)
[Finished in 0.631s]
在上面的例子中,ATOM 错误可能是因为我没有导入 Jasmine 模块?也许 Repl.it 错误只是一个 Repl.it 问题。只是想我会回来看看是否有任何额外的见解给了新信息。

最佳答案

复制的输出应该如下

[
{ commandType: 'MODE_CHANGE', value: 'LOW_POWER' },
{ commandType: 'STATUS_CHECK', value: undefined }
]

The prefix Command is just the name of the object which is displayedas part of better logging. The syntax does not consist of this.

关于javascript - 在单元测试中接收语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63319554/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com