我在这里有一个简单的测试,我希望它能工作。我们的想法是让phantom在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)当我从这个测试中删除phantom时,它可以正常工作。
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在这个框架中工作。有谁可以帮我?
发布于 2013-01-04 07:28:51
我问的问题是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()
)https://stackoverflow.com/questions/14148824
复制相似问题