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.