سؤال

I'm running test using mocha-phantomjs.

While running an unit test, I'm not running http server, so my resources are not available through absolute urls (eg: /static/images/face.png), and I'm getting following output:

% 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

My test:

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

The test succeed, but in the mean time I have this ugly error messages. How to configure phantomJS (or mocha?) to ignore loading that resources / not presenting this error messages?

هل كانت مفيدة؟

المحلول

You have to disable the loadImages default mocha-phantomjs -s loadImages=false

نصائح أخرى

The answer by @reubano i.e. setting loadImages=false prevents phantomjs from loading images at all, and thus is what @robert-zaremba was looking for. However, if you need to unit test how your code reacts to missing files, you want the files to be really loaded. In that case the error messages will annoy you because testing for missing files is a part of your test suite.

Fortunately mocha-phantomjs has a flag --ignore-resource-errors. It is your saviour. The files are still loaded but the annoying errors are gone.

If you use phantomjs directly a solution would be to override page.onResourceError described here.

You could use a window.onError(function() {... handler to catch (and eat) those errors.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top