Casper require()与CoffeeScript不导入外部文件

时间:2015-04-23 15:53:33

标签: coffeescript require casperjs

我跟着CasperJS's documentation关于在主Casper测试文件中包含.coffee个文件。我的代码如下所示:

家/测试/ my_test_file.coffee:

parameters = require('../parameters')

casper.test.begin "Test ", (test) ->

    home_page = parameters.root_path
    page_to_test = home_page + "my_page_to_test"

    casper.start page_to_test, ->
        test.assertEquals @getCurrentUrl(), page_to_test

    casper.run ->
        test.done()

家/ parameters.coffee:

require = patchRequire global.require

root_path = "http://localhost:1080/"
my_page = "foo"
other_param = "bar"

exports = ->
    {
        'root_path': root_path,
        'my_page': my_page,
        'other_param': other_param
    }

但是,Casper一直告诉我page_to_testmy_test_file.coffee未定义。

1 个答案:

答案 0 :(得分:1)

这不正确使用exports。首先,您不需要此处的函数,因为您可以直接访问返回对象的属性。第二,你无法直接向exports分配内容。这是module.exports的用途。

module.exports = {
    'root_path': root_path,
    'my_page': my_page,
    'other_param': other_param
}

exports.root_path = root_path
exports.my_page = my_page
exports.other_param = other_param

通过将对象分配给exportsexports = obj),您将覆盖执行实际导出的对象,并且不会导出任何内容。