2012-12-05 42 views
0

我是新的node.js,我正在嘗試在一個項目中加入一個摩卡測試套件。我目前所面對的問題是,當與mocha,node.js,ReferenceError:董事會沒有定義

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

公共/ Java腳本/ 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; 
} 

公共/ JavaScript的運行我的測試以下

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) 
. . . 

/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.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

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); 

正如你所看到的錯誤是在game.js:8的事情是,我不知道如何正確配置它,因爲當遊戲正在播放時,它可以正常工作,這意味着new Game()工作正常,問題在於我沒有從測試套件中正確配置它。我會感謝任何幫助。提前致謝。

回答

0

從你提供我的猜測的代碼是你需要require board.js從game.js像這樣:

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

它看起來這是一個瀏覽器的遊戲,你還測試來自node.js.那是對的嗎?

相關問題