引用错误,魔法未定义。但它是在测试参数中定义的吗?
Reference Error, Magic is not defined. But it is defined in the test parameters?
我正在练习这个型:https://www.codewars.com/kata/magic-the-gathering-number-3-spell-stack/train/javascript。
它在 运行 样本测试中抛出此错误:
ReferenceError: Magic is not defined
at /home/codewarrior/index.js:7:1
at Object.handleError
<anonymous>
我研究了原型的工作原理,我的代码似乎是正确的。
到目前为止,这是我的代码:
// Create the Magic object with a spellStack method
// Happy Casting!
Magic.prototype.spellStack = (card) => {
console.log(card);
}
测试参数是这样的:
Test.describe("It should add and remove spells on the stack", function() {
let spells = [{'name':'Lightning Bolt', 'type': 'instant'}, {'name': 'Giant Growth', 'type': 'instant'},
{'name':'Time Walk', 'type': 'sorcery'}, {'name': 'Ponder', 'type': 'sorcery'}];
var myMagic = new Magic();
Test.assertSimilar(myMagic.spellStack(spells[2]), 1);
Test.assertSimilar(myMagic.spellStack(spells[0]), 2);
Test.assertSimilar(myMagic.spellStack(spells[0]), 3);
Test.assertSimilar(myMagic.spellStack(spells[1]), 4);
Test.assertSimilar(myMagic.spellStack(), spells[1]);
Test.assertSimilar(myMagic.spellStack(), spells[0]);
Test.assertSimilar(myMagic.spellStack(), spells[0]);
Test.it("Should throw an error if trying to add a sorcery to a stack with spells:", function() {
Test.expectError(()=>myMagic.spellStack(spells[3]));
Test.expectError(()=>myMagic.spellStack(spells[2]));
});
Test.it("Should throw an error if trying to retrieve a spell when the stack is empty:", function() {
Test.assertSimilar(myMagic.spellStack(), spells[2], "Removing the last spell from the stack");
Test.expectError(()=>myMagic.spellStack());
});
});
至少它应该给出一个错误,但也可以控制函数输入。
这条线
Magic.prototype.spellStack = (card) => {
你在原型上设置了一个属性,但是Magic
在哪里定义的? Magic
还不存在。
你需要先定义它
function Magic() {}
Magic.prototype.spellStack = (card) => {
console.log(card);
}
我正在练习这个型:https://www.codewars.com/kata/magic-the-gathering-number-3-spell-stack/train/javascript。
它在 运行 样本测试中抛出此错误:
ReferenceError: Magic is not defined
at /home/codewarrior/index.js:7:1
at Object.handleError
<anonymous>
我研究了原型的工作原理,我的代码似乎是正确的。
到目前为止,这是我的代码:
// Create the Magic object with a spellStack method
// Happy Casting!
Magic.prototype.spellStack = (card) => {
console.log(card);
}
测试参数是这样的:
Test.describe("It should add and remove spells on the stack", function() {
let spells = [{'name':'Lightning Bolt', 'type': 'instant'}, {'name': 'Giant Growth', 'type': 'instant'},
{'name':'Time Walk', 'type': 'sorcery'}, {'name': 'Ponder', 'type': 'sorcery'}];
var myMagic = new Magic();
Test.assertSimilar(myMagic.spellStack(spells[2]), 1);
Test.assertSimilar(myMagic.spellStack(spells[0]), 2);
Test.assertSimilar(myMagic.spellStack(spells[0]), 3);
Test.assertSimilar(myMagic.spellStack(spells[1]), 4);
Test.assertSimilar(myMagic.spellStack(), spells[1]);
Test.assertSimilar(myMagic.spellStack(), spells[0]);
Test.assertSimilar(myMagic.spellStack(), spells[0]);
Test.it("Should throw an error if trying to add a sorcery to a stack with spells:", function() {
Test.expectError(()=>myMagic.spellStack(spells[3]));
Test.expectError(()=>myMagic.spellStack(spells[2]));
});
Test.it("Should throw an error if trying to retrieve a spell when the stack is empty:", function() {
Test.assertSimilar(myMagic.spellStack(), spells[2], "Removing the last spell from the stack");
Test.expectError(()=>myMagic.spellStack());
});
});
至少它应该给出一个错误,但也可以控制函数输入。
这条线
Magic.prototype.spellStack = (card) => {
你在原型上设置了一个属性,但是Magic
在哪里定义的? Magic
还不存在。
你需要先定义它
function Magic() {}
Magic.prototype.spellStack = (card) => {
console.log(card);
}