我正在使用摩卡-幻影运行测试。
在运行单元测试时,我没有运行http服务器,因此我的资源无法通过绝对urls (例如:/静态/映像/face.png)获得,并且得到以下输出:
% node_modules/mocha-phantomjs/bin/mocha-phantomjs test/runner/runner.html
test app.Main
✓ should show a command line
1 test complete (79 ms)
Error loading resource file:///static/images/face.png (203). Details: Error opening /static/images/face.png: No such file or directory我的测试:
describe "test app.Main", ->
beforeEach ->
@app = App.Main()
React.renderComponent @app, root[0]
afterEach -> React.unmountComponentAtNode root[0]
it "should show a command line", ->
@cmdForm = root.find("div.header-block").first().find('form')
expect(@cmdForm.length).to.equal 1
expect(@cmdForm.find('div.select2-container ul.select2-choices').length).equal 1测试成功了,但同时我也收到了一些丑陋的错误信息。如何配置phantomJS (或mocha?)忽略加载资源/不显示此错误消息?
发布于 2014-05-10 13:27:12
您必须禁用loadImages默认mocha-phantomjs -s loadImages=false
发布于 2016-03-28 11:08:50
@reubano的答案,即设置loadImages=false,可以防止幻影加载图像,这就是@robert-zaremba所要寻找的。但是,如果您需要单元测试您的代码如何对丢失的文件作出反应,您希望真正加载这些文件。在这种情况下,错误消息会惹恼您,因为对丢失文件的测试是测试套件的一部分。
幸运的是,摩卡-幻影有一个标志--ignore-resource-errors。这是你的救世主。文件仍然被加载,但恼人的错误消失了。
如果直接使用幻影,解决方案将是覆盖page.onResourceError 在此描述。
发布于 2014-05-05 18:55:34
您可以使用window.onError(function() {...处理程序来捕获(并吃掉)这些错误。
https://stackoverflow.com/questions/23245547
复制相似问题