2012-03-17 36 views
1

我在測試flatironcli appMocha時遇到問題。使用Mocha測試flatiron cli應用程序:app.log未定義?

我想測試的命令行命令將創建一個目錄並使用app.log.info記錄成功。

這是代碼進行測試(./lib/commands/create.js):

var flatiron = require('flatiron'), 
    app = flatiron.app, 
    fs = require('fs'), 
    path = require('path'); 

module.exports = function create(name, callback) { 
    "use strict"; 
    fs.mkdir('./' + name); 
    app.log.info('Directory created!'); 
} 

這是測試(./test/create.js):

var create = require('../lib/commands/create'); 

describe('Flatiron command', function() { 
    "use strict"; 
    describe('#create()', function() { 
    it('should create a directory ', function() { 
     create('someDirectory'); 
     // check if the directory was created, 
     // then remove the directory 
    }); 
    }); 
}); 

mocha test/log -R spec給我

Flatiron command 
    #log() 
     1) should log something 


    ✖ 1 of 1 tests failed: 

    1) Flatiron command #create() should create a directory : 
    TypeError: Cannot call method 'info' of undefined 

爲什麼app.log不可摩卡?

這是因爲function log如何導出?

或者這與熨斗如何設置應用程序有關?我想需要flatiron.app從測試開始就這樣

var create = require('../lib/commands/create'), 
    flatiron = require('flatiron'), 
    app = flatiron.app; 

describe('Flatiron command', function() { 
    "use strict"; 
    describe('#create()', function() { 
    it('should create a directory ', function() { 
     app.start(); 
     create('someDirectory'); 
    }); 
    }); 
}); 

- 但沒有成功,只是一個不同的錯誤:

Flatiron command 
    #create() 
     1) should create a directory 


    ✖ 1 of 1 tests failed: 

    1) Flatiron command #create() should create a directory : 
    TypeError: Object [object Object] has no method 'start' 

或者這是你會使用間諜的情況下/存根/模擬類似於sinon.js來模擬app.log的行爲?因爲如果日誌記錄正常工作,我並不感興趣,但是如果創建目錄。

回答

1

好吧,我明白了。

使用app.start()並不完全 - 但它適用於app.init()。 在烙鐵,app.init()通常從主文件中通過插入flatiron.plugins.cli叫成app.use()這樣的:

var flatiron = require('flatiron'), 
    path = require('path'), 
    app = flatiron.app; 

app.config.file({ file: path.join(__dirname, '..', 'config', 'config.json') }); 

app.use(flatiron.plugins.cli, { 
    dir: path.join(__dirname, '..', 'lib', 'commands'), 
    usage: 'Empty Flatiron Application, please fill out commands' 
}); 
app.start(); 

調用app.init()sets up loggingwinston,烙鐵記錄插件。

但是你可以在測試中調用app.init()而不用在它後面調用app.start()。 所以此工程:

var create = require('../lib/commands/create'), 
    flatiron = require('flatiron'), 
    app = flatiron.app; 

describe('Flatiron command', function() { 
    "use strict"; 
    describe('#create()', function() { 
    it('should create a directory ', function() { 
     app.init(); 
     create('someDirectory'); 
    }); 
    }); 
}); 

摩卡甚至需要記錄的護理:

Flatiron command 
    #create() 
     ◦ should create a directory : info: Directory created! 
     ✓ should create a directory (48ms) 


    ✔ 1 tests complete (50ms) 

如果要停止日誌記錄,您可以使用app.log.loggers.default.remove(winston.transports.Console)你叫app.init()後。你必須要求溫斯頓這樣做。

+0

#init()方法來自百老匯。您必須初始化百老匯應用程序才能獲得依賴注入功能。 – srquinn 2013-04-20 20:12:43

相關問題