0

我在这里有一个简单的测试,我希望能工作。这个想法是让幻影在 mocha 的 tdd 中工作。(顺便说一句,我尝试了 node-phantom 和 phantomjs-node 都没有成功。)

nodephantom = require 'node-phantom'
assert    = require("chai").assert

host = 'http://google.com'

phantom = null
page = null

suite "Mocha Node-Phantom Loading Google:", ->

  suiteSetup (done) ->
    nodephantom.create (err,p) ->
      phantom = p
      done()

  test "Google page should load", (done) ->
    phantom.createPage (p) ->
      page = p
      page.open host, ->
        assert.match page.content(), /google/, "Page is loaded"
        done()

  suiteTeardown ->
    phantom.exit()

我执行代码如下:

mocha -u tdd -R tap -b sometest.coffee

我收到以下错误:

1..1
not ok 1 Mocha Node-Phantom Loading Google: "before all" hook
  Error: global leak detected: location
      at Runner.checkGlobals (/home/ericstob/b/nodejs/lib/node_modules/mocha/lib/runner.js:104:21)
      at Runner.<anonymous> (/home/ericstob/b/nodejs/lib/node_modules/mocha/lib/runner.js:44:44)
      at Runner.EventEmitter.emit (events.js:88:17)
      at Runner.hook (/home/ericstob/b/nodejs/lib/node_modules/mocha/lib/runner.js:170:12)
      at done (/home/ericstob/b/nodejs/lib/node_modules/mocha/lib/runnable.js:134:5)
      at Runnable.run.duration (/home/ericstob/b/nodejs/lib/node_modules/mocha/lib/runnable.js:146:9)
      at phantom.createPage.page (/home/ericstob/b/seo/t/sometest.coffee:18:16)
      at SocketNamespace.module.exports.create (/home/ericstob/b/nodejs/lib/node_modules/node-phantom/node-phantom.js:156:4)
      at SocketNamespace.EventEmitter.emit [as $emit] (events.js:115:20)
      at connect (/home/ericstob/b/nodejs/lib/node_modules/node-phantom/node_modules/socket.io/lib/namespace.js:292:10)
      at /home/ericstob/b/nodejs/lib/node_modules/node-phantom/node_modules/socket.io/lib/namespace.js:308:13
      at SocketNamespace.authorize (/home/ericstob/b/nodejs/lib/node_modules/node-phantom/node_modules/socket.io/lib/namespace.js:252:5)
      at SocketNamespace.handlePacket (/home/ericstob/b/nodejs/lib/node_modules/node-phantom/node_modules/socket.io/lib/namespace.js:302:14)
      at Manager.handleClient (/home/ericstob/b/nodejs/lib/node_modules/node-phantom/node_modules/socket.io/lib/manager.js:697:32)
      at Manager.handleUpgrade (/home/ericstob/b/nodejs/lib/node_modules/node-phantom/node_modules/socket.io/lib/manager.js:618:8)
      at Server.<anonymous> (/home/ericstob/b/nodejs/lib/node_modules/node-phantom/node_modules/socket.io/lib/manager.js:123:10)
      at Server.EventEmitter.emit (events.js:115:20)
      at Socket.socket.ondata (http.js:1710:14)
      at TCP.onread (net.js:403:27)

当我从此测试中删除幻像时,它按预期工作。

assert    = require("chai").assert

is_decimal = (val) -> assert.match String(val), /^-?[0-9]*\.?[0-9]+$/, String(val) + ' is a decimal.'
is_hex = (val) -> assert.match String(val), /^(0[xX])?[a-fA-F0-9]+$/, String(val) + ' is a hexadecimal.'

suite "Mocha Minimal Test:", ->

  suiteSetup (done) ->
    done()

  test "Assertions pass", (done) ->
    is_decimal 1.5
    is_hex "0x2fc3"
    done()

  test "Assertions fail", (done) ->
    is_decimal 'dog'
    done()

  suiteTeardown ->
    {}

结果:

$ mocha -u tdd -R tap -b mintest.coffee
1..2
ok 1 Mocha Minimal Test: Assertions pass
not ok 2 Mocha Minimal Test: Assertions fail
  'dog' is a decimal.: expected 'dog' to match /^-?[0-9]*\.?[0-9]+$/

所以我知道我的测试的基本结构是正确的,mocha 很高兴。但是 node-phantom 对此有些不满。

我只想让 phantomjs 在这个框架内工作。谁能帮我?

4

1 回答 1

1

我问的问题在这里得到了回答:

答案是,如果一个库声明了全局变量,Mocha 就会崩溃。我需要包含一个像这样的 -globals 参数:

mocha -u tdd -R tap --globals location -b sometest.coffee

为了允许 phantom 声明位置变量。

另外,我不得不稍微更改我的代码,因为 node-phantom 不支持 page.content()

test "Google page should load", (done) ->
  phantom.createPage (err,p) ->
    page = p
    page.open host, ->
      page.evaluate( 
        -> return document.documentElement.innerHTML
        (err, result) ->
          assert.match result, /google/, "Page is loaded"
          done()
      )
于 2013-01-03T23:28:51.067 回答