gpt4 book ai didi

javascript - 无法将 undefined 或 null 转换为对象

转载 作者:搜寻专家 更新时间:2023-11-01 00:31:02 26 4
gpt4 key购买 nike

不知道我哪里错了:

test.js

let chai = require('chai'),
should = chai.should(),
game = require('../src/game');

it('should be able to start the game', () => {
game.start();

game.started.should.be.true;
});

game.js

var board = require('./board'),
hasStarted = false;

module.exports = {
start: start,
started: hasStarted
};

function start(){
hasStarted = true;
};

对于测试,我得到断言错误:

AssertionError: expected false to be true

我以为我已经在我的 start() 方法中设置了它,但为什么我的测试仍然因 false 而失败?

最佳答案

您已将 hasStarted 的初始值分配给您的模块导出,它不会随着对 start() 的调用而改变。

使用函数而不是变量来检索它,即:

module.exports = {
start: start,
started: function() { return hasStarted; }
};

关于javascript - 无法将 undefined 或 null 转换为对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33981225/

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