6

I have a capybara test that checks for content on a page. I have an img tag thats src calls for a URL that does not exist. When I run the test I receive:

Failure/Error: raise ActionController::RoutingError, "No route matches [#{env['REQUEST_METHOD']}] #{env['PATH_INFO'].inspect}"

  ActionController::RoutingError:
    No route matches [GET] "/avatars/original/missing.png"

I honestly don't care about this request. Is there any way for me stub /avatars/original/missing.png on my Capybara test?

thank_you
  • 11,001
  • 19
  • 101
  • 185
  • Check http://stackoverflow.com/questions/14629491/capybara-tests-with-js-true-routing-error-no-route-matches-get-assets – Sully Aug 26 '16 at 13:55
  • @Hitham Although somewhat helpful, I couldn't find a direct answer to my question. In all of those cases, they were trying to fix their asset pipeline. I don't care about that. I just want to stub it out. – thank_you Aug 26 '16 at 13:59
  • Fix your asset pipeline so you don't have to stub out something that you shouldn't have to stub. – ruby_newbie Aug 26 '16 at 15:10
  • @ruby_newbie, Let me further explain. There is nothing wrong with my asset pipeline. My pipeline works perfectly. The problem is is that the img src is being retrieved from a database record which I have no control in changing. So either I create a route for this image or stub it. I would prefer to stub it. – thank_you Aug 26 '16 at 15:31
  • What driver are you using? – Thomas Walpole Aug 26 '16 at 16:45
  • @TomWalpole, poltergeist. – thank_you Aug 26 '16 at 16:45
  • You can use the blacklist functionality in poltergeist to block those url requests. If using 1.10+ you can do it in the driver config - otherwise per test in a before block – Thomas Walpole Aug 26 '16 at 16:47
  • @TomWalpole, this is a great direction. So far I'm determining how to set it so it blacklists relative url links. My url is pointing to within the app itself. – thank_you Aug 26 '16 at 16:59
  • @TomWalpole, got it to work. Ended up using a before block in the test itself. Post as an answer and I'll give you credit. – thank_you Aug 26 '16 at 17:04

1 Answers1

4

When using Poltergeist you can use the blacklist functionality to block specific requests. If using Poltergeist 1.10.0+ you can configure it for every test in the driver registration block by adding the :url_blacklist option

Capybara.register_driver :poltergeist do |app|
  #  Change domain name as necessary
  options = { url_blacklist: ['http://www.example.com/avatars/original/missing.png'] } # you can also use * as a wildcard
  Capybara::Poltergeist::Driver.new(app, options)
end

In pre 1.10 or if you want to do it on a test by test/before block basis you can do

page.driver.browser.url_blacklist = ['http://www.example.com/avatars/original/missing.png']
Thomas Walpole
  • 48,548
  • 5
  • 64
  • 78
  • 1
    Do you have any idea how can I blacklist to black a request when using Capybara.default_driver = :selenium – Am33d Jul 16 '18 at 15:26