2012-03-20 29 views
26

Tôi đang cố gắng chạy một số thử nghiệm bằng cách sử dụng mocha nhưng dường như không thể vượt qua lỗi này.nodejs mocha suite không được xác định lỗi

E:\tdd\nodejs\cart>mocha cart.test.js 

node.js:201 
     throw e; // process.nextTick error, or 'err 
      ^
ReferenceError: suite is not defined 
    at Object.<anonymous> (E:\tdd\nodejs\cart\cart.test.js:5:1 
    at Module._compile (module.js:432:26) 
    at Object..js (module.js:450:10) 
    at Module.load (module.js:351:31) 
    at Function._load (module.js:310:12) 
    at Module.require (module.js:357:17) 
    at require (module.js:368:17) 
    at C:\Users\lex\AppData\Roaming\npm\node_module 
    at Array.forEach (native) 
    at load (C:\Users\lex\AppData\Roaming\npm\node_ 
9) 
    at Object.<anonymous> (C:\Users\lex\AppData\Roa 
in\_mocha:237:1) 
    at Module._compile (module.js:432:26) 
    at Object..js (module.js:450:10) 
    at Module.load (module.js:351:31) 
    at Function._load (module.js:310:12) 
    at Array.0 (module.js:470:10) 
    at EventEmitter._tickCallback (node.js:192:40) 

Từ những gì tôi có thể biết từ ngăn xếp cuộc gọi vấn đề ở đây cart.test.js:5:1. Bất kỳ ý tưởng nào gây ra điều này?

Cảm ơn

cart.js

var GetTotalSum = function (input) { 
    var total = 0, 
     differentTitles = 0, 
     discountMap = [0, 1, 0.95, 0.9, 0.8, 0.75], 
     BOOK_PRICE = 8; 

    for (var i in input) { 
     total += input[i] * BOOK_PRICE; 
     if (input[i] > 0) { 
      differentTitles++; 
     } 
    } 

    if (differentTitles > 1) { 
     total = total * discountMap[differentTitles]; 
    } 

    return total; 
} 


module.exports.GetTotalSum = GetTotalSum; 

cart.test.js

var assert = require('assert'), 
    cart = require('./cart.js'); 


suite('cart', function() { 
    test('buy one book', function() { 
     // Arrange 
     var input = [1, 0, 0, 0, 0], 
      expected = 8; 

     // Act 
     var actual = cart.GetTotalSum(input); 

     // Assert 
     assert.equal(actual, expected);  
    }); 
}); 

Trả lời

53

Bạn cần phải nói với Mocha sử dụng giao diện TDD, thay vì mặc định BDD một:

mocha --ui tdd card.test.js 
+0

cảm ơn. hoạt động tốt. Bất kỳ cách nào để chỉ định giao diện tdd như mặc định? – thedev

+1

Sử dụng tệp 'mocha.opts', xem http://visionmedia.github.com/mocha/ để biết chi tiết. – Domenic

+0

bạn sẽ làm điều đó như thế nào? Tôi chạy thử nghiệm của tôi trên mỗi lưu, nó được thiết lập theo cách đó thông qua gulp. Tôi đang chạy cả thử nghiệm BDD và TDD – PositiveGuy

3

Bạn cũng có thể bao gồm một Makefile trong dự án của bạn và chỉ định TDD như vậy:

test: 
    @./node_modules/.bin/mocha -u tdd 

.PHONY: test 

Hat tip: DailyJS

3

Bạn có thể làm như vậy bằng cách chỉ định mocha -u tdd trong package.json

"scripts": { 
"start" : "node server",  
"test": "mocha -u tdd" 
} 
Các vấn đề liên quan