我有一个用Coffeescript编写的web应用程序,我正在使用nodeunit进行测试,但我似乎无法访问在测试中设置的全局变量(应用程序中的“session”变量):
src/test.cafee
root = exports ? this
this.test_exports = ->
console.log root.export
root.exporttest/test.cafee
exports["test"] = (test) ->
exports.export = "test"
test.equal test_file.test_exports(), "test"
test.done()输出结果:
test.coffee
undefined
✖ test
AssertionError: undefined == 'test'如何跨测试访问全局变量?
发布于 2012-01-16 20:48:51
为节点创建导出的伪window全局:
src/window.cafee
exports["window"] = {}src/test.cafee
if typeof(exports) == "object"
window = require('../web/window')
this.test_exports = ->
console.log window.export
window.exporttest/test.cafee
test_file = require "../web/test"
window = require "../web/window'"
exports["test"] = (test) ->
window.export = "test"
test.equal test_file.test_exports(), "test"
test.done()不是很优雅,但很管用。
发布于 2012-01-16 02:40:41
您可以使用" global“对象共享全局状态。
one.coffee:
console.log "At the top of one.coffee, global.one is", global.one
global.one = "set by one.coffee"two.coffee:
console.log "At the top of two.coffee, global.one is", global.one
global.two = "set by two.coffee"从第三个模块加载每个模块(本例中为交互式会话)
$ coffee
coffee> require "./one"; require "./two"
At the top of one.coffee, global.one is undefined
At the top of two.coffee, global.one is set by one.coffee
{}https://stackoverflow.com/questions/8870634
复制相似问题