0

nodeunitでテストしているCoffeescriptで記述されたWebアプリがあり、テストで設定されたグローバル変数(アプリの「セッション」変数)にアクセスできないようです。

src / test.coffee

root = exports ? this

this.test_exports = ->
    console.log root.export
    root.export

test / test.coffee

exports["test"] = (test) ->
    exports.export = "test"
    test.equal test_file.test_exports(), "test"
    test.done()

出力の結果:

test.coffee
undefined
✖ test

AssertionError: undefined == 'test'

テスト間でグローバルにアクセスするにはどうすればよいですか?

4

2 に答える 2

1

ノードの偽のwindowエクスポートされたグローバルを作成します。

src / window.coffee

exports["window"] = {}

src / test.coffee

if typeof(exports) == "object"
    window = require('../web/window')

this.test_exports = ->
    console.log window.export
    window.export

test / test.coffee

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-16T12:48:51.380 に答える
0

「グローバル」オブジェクトを使用してグローバル状態を共有できます。

one.coffee:

console.log "At the top of one.coffee, global.one is", global.one
global.one = "set by one.coffee"

2.コーヒー:

console.log "At the top of two.coffee, global.one is", global.one
global.two = "set by two.coffee"

3 番目のモジュール (この例では対話型セッション) からそれぞれをロードします。

$ 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
{}
于 2012-01-15T18:40:40.790 に答える