2

In my spec I use AssetUrlHelper:

include ActionView::Helpers::AssetUrlHelper

so that I can test a page that should display an absolute URL to one of my files. I thought asset_url should do the trick:

asset_url(img.img_file(:medium))

Unfortunately while the spec runs this has no host name, e.g.

(byebug) asset_url("blurg.jpg")
"/images/blurg.jpg"

... not even if I specify the host name as an option:

(byebug) asset_url("blurg.jpg", host: "myhost.com")
"/images/blurg.jpg"

At the very least the second case is odd in that it contradicts this comment in the source:

# If :host options is set, it overwrites global
# +config.action_controller.asset_host+ setting.
#
# ...
#
#   asset_url "application.js"                                 # => http://example.com/assets/application.js
#   asset_url "application.js", host: "http://cdn.example.com" # => http://cdn.example.com/assets/application.js

This has lead to a bug going unspotted until now. My test thinks there is no difference between the relative file path which my view outputs (wrongly) and the absolute url that should be there:

myhost.com/images/blurg.jpg #what I expect the test to check for
/images/blurg.jpg #what the test actually checks for

The contents of the view do not matter. I know how to fix it. I want to know for this case and for future reference, why does this helper not behave as I expect in the test, and how do I fix the test.

iftheshoefritz
  • 5,829
  • 2
  • 34
  • 41

1 Answers1

0

A feature spec doesn't have the necessary Rails framework loaded in order to calculate the host. I'm a little surprised that providing the :host option doesn't work, but not hugely because there's a lot of smarts inside Rails concerning the URLs referring to the routing, resolving lots of things, and generally trying to be very smart.

If you switch over to a controller spec, then you'll find that the proper context is stubbed enough that asset_url works as advertised (without the :host option).

smathy
  • 26,283
  • 5
  • 48
  • 68