简体   繁体   English

摩卡,node.js,ReferenceError:未定义板

[英]mocha, node.js, ReferenceError: Board is not defined

I'm new with node.js and I'm trying to collaborate in a project adding a mocha test suite for it. 我是node.js的新手,我正尝试在一个项目中进行协作,为其添加一个摩卡测试套件。 The issue that I have at the moment is the following 我目前遇到的问题如下

ReferenceError: Board is not defined
at new Game (/Users/.../dr_mojo/public/javascripts/game.js:8:20)
at Context.<anonymous> (/Users/.../dr_mojo/test/test.game.js:13:17)
at Test.Runnable.run (/usr/local/lib/node_modules/mocha/lib/runnable.js:213:32)
at Runner.runTest (/usr/local/lib/node_modules/mocha/lib/runner.js:343:10)
at Runner.runTests.next (/usr/local/lib/node_modules/mocha/lib/runner.js:389:12)
. . .

when run my test with 当我运行测试

$> mocha -u tdd test/test.game.js --reporter spec

public/javascripts/board.js public / javascripts / board.js

function Board(width, height) {
  this.board = new Array(width);
  this.width = width;
  this.height = height;
  for( var i = 0; i < width ; ++i) {
    this.board[i] = new Array(height);
  }
}
...
if(typeof module != 'undefined') {
  module.exports.Board = Board;
}

public/javascripts/game.js public / javascripts / game.js

function Game(lvl, speed, music) {
  this.initial = { ... };
  this.board = new Board(board_size[0], board_size[1]);
  ...
}
...
if(typeof module != 'undefined') {
  module.exports.Game = Game;
}

test/test.game.js 测试/test.game.js

var assert = require("assert");
var Board  = require(__dirname + "/../public/javascripts/board.js").Board;
var Pill   = require(__dirname + "/../public/javascripts/pill.js").Pill;
var Game   = require(__dirname + "/../public/javascripts/game.js").Game;

describe('Game', function(){
  it('Clears a row', function(){
    var game  = new Game();
    var pill1 = new Pill(game.board, game.detector, [ {x : 0 , y : 0 }, {x : 1, y : 0 } ],["red", "red"]);
    var pill2 = new Pill(game.board, game.detector, [ {x : 2 , y : 0 }, {x : 3, y : 0 } ],["red", "red"]);

    assert.equal(game.board.matches().length, 1);

    game.findMatches(function(){});
    assert.equal(game.board.matches().length, 0);
  })
})

server.js server.js

var express = require('express'),
    port    = 8888;

var app = express.createServer();

app.use(express.static(__dirname + '/public'));
app.set("view engine", "jade");
app.set('view options', { layout: false });

app.get('/play', function(req, res){
  res.render('play_game');
});

app.listen(port);

As you can see the error is in game.js:8 the thing is that I don't know how to configure it properly given that when the game is playing it works ok, this means that new Game() works ok and the problem is that I'm not configuring it properly from the test suite. 如您所见,错误是在game.js:8发生的,原因是我不知道如何正确配置它,因为在玩游戏时它可以正常运行,这意味着new Game()可以正常运行,而且问题出在我没有从测试套件正确配置它。 I'll appreciate any help. 我将不胜感激。 Thanks in advance. 提前致谢。

From the code you provided my guess is that you'll need to require board.js from game.js like so: 根据您提供的代码,您需要像以下这样从game.js中require board.js:

var Board  = require(__dirname + "/../public/javascripts/board.js").Board;

It looks like this is a browser game that you're also testing from node.js. 看来这是您也在node.js中测试的浏览器游戏。 Is that correct? 那是对的吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM