我在这里有一个简单的测试,我希望能够工作。我的想法是在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]+$/
所以我知道我的测试的基本结构是正确的,摩卡很高兴。但有一些关于它的节点 - 幻影不满意。
我只想让phantomjs在这个框架内工作。任何人都可以帮助我吗?
答案 0 :(得分:1)
我问的问题已经回答here:
答案是如果一个库声明全局变量,Mocha会吓坏。 我需要包含一个像这样的-globals参数:
mocha -u tdd -R tap --globals location -b sometest.coffee
以允许幻像声明位置变量。
另外,我不得不稍微更改我的代码,因为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()
)