3
請考慮,我有以下的CoffeeScript代碼:奇怪fs.readFile行爲
class Foobar
test: (path) ->
fs = require 'fs'
fs.readFile path, (err, data) ->
console.log 'fs.readFile callback fired'
root = exports ? window
root.Foobar = Foobar
而下面的測試文件的摩卡:
chai = require 'chai'
expect = chai.expect
chai.should()
{Foobar} = require '../source/foobar'
describe 'Foobar', ->
foobar = null
it 'does nothing', ->
foobar = new Foobar
foobar.test 'foobar.txt'
運行測試:
mocha --compilers coffee:coffee-script -R spec
奇怪的是我的控制檯沒有記錄任何東西。當我改變我的咖啡這(加在最後兩行):
class Foobar
test: (path) ->
fs = require 'fs'
fs.readFile path, (err, data) ->
console.log 'fs.readFile callback fired'
root = exports ? window
root.Foobar = Foobar
foobar = new Foobar
foobar.test 'foobar.txt'
運行測試,現在控制檯日誌fs.readFile callback fired
兩次,符合市場預期。
那麼,爲什麼控制檯在第一種情況下是空的?