【问题标题】:Casper require() with CoffeeScript not importing external files带有 CoffeeScript 的 Casper require() 不导入外部文件
【发布时间】:2015-04-23 15:53:33
【问题描述】:

我关注了 CasperJS's documentation 的内容,包括来自主 Casper 测试文件的 .coffee 文件。我的代码如下所示:

home/tests/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()

home/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 中未定义。

【问题讨论】:

    标签: coffeescript require casperjs


    【解决方案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
    

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

    【讨论】:

      猜你喜欢
      • 2020-05-18
      • 2021-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-24
      • 2011-06-13
      • 1970-01-01
      • 2013-11-03
      相关资源
      最近更新 更多