New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Default_url_options is being ignored #1275
Comments
What problem is this solving for you? I'd like to understand where this is causing an issue. |
@cupakromer The problem is; when I set default_url_options in my spec/rails_helper.rb or environments/test.rb. It's being ignored in feature specs. I set like following; Rails.application.routes.default_url_options[:host] = 'lvh.me' But in a feature spec; |
Ok, I dug into this a bit. It's a little more complicated that it seems. Here's what is going on. The Rails test cases, the RSpec example groups are a thin shim on top of these, setup an attribute If we removed or comment out the line: default_url_options[:host] ||= ::RSpec::Rails::FeatureExampleGroup::DEFAULT_HOST You would get the following error:
The reason your proposed solution in #1276 appears to work is simply because the example's So unfortunately, your solution is basically a no-op and is the same as deleting that We could change it to: if respond_to?(:default_url_options)
default_url_options[:host] ||= (
app.routes.default_url_options[:host] ||
::RSpec::Rails::FeatureExampleGroup::DEFAULT_HOST
)
end However, this will cause other problems: require 'rails_helper'
Rails.application.routes.default_url_options[:host] = 'lvh.me'
RSpec.feature "Relative path hosts set by Capybara", type: :feature do
it "causes a mismatch between the URLs" do
visit widgets_path
expect(current_url).to eq widgets_url
end
end Results in a failure:
This is because Capybara uses There's another wrinkle. Capybara expects the "http://" part in the URL, if it's not present the URL is created as: "http:///widgets". I'm not sure this is a bug, you'd have to ask them. So while Rails is happy to have "lvh.me", Capybara would need "http://lvh.me". We are tightly coupled to Capybara right now. As you can see from the feature example group file it's essentially just all code necessary to handle Capybara's design decisions. It also causes a lot of confusion for newer users who do not understand this tight dependency. I really do not want to add yet another special case for Capybara. Let me think on this a bit. I have a potential solution in mind but will need to think on it for a day. I hope this helps understand what's happening. |
Any fix? |
Not yet, see @cupakromer's comments above, you can try setting the Capybara default host as a work around? |
@JonRowe I'm not using Capybara. Same thing in controller tests. |
This issue only affects feature tests, which use Capybara. |
Again. This issue in controllers tests too. [ApplicationController, ActionController::Base].each do |klass|
klass.class_eval do
def default_url_options(options = {})
{ :host => "example.com" }.merge(options)
end
end
end |
Can you elaborate what symptoms you are seeing; It's possible you are suffering from the symptoms of the rails behaviour (which we cannot fix) or a different bug altogether. |
I'm testing subdomains and instead of _path helpers using _url in my controller. It's only what I set. |
Sorry you're going to have to elaborate further, when reporting issues it's best to actually provide an example showing what you're trying to do, and the unexpected outcome of that attempt. Even better if it's actually executable (but please... don't point me to a rails app repo, I don't have time to comb through an entire app). |
I'm still having lots of problems when testing Any thoughts? |
Facing the same issue in features spec with Rspec and Capybara. Tried adding the config in application.rb Further tried overriding Capybara config
But 'about_us_url' returns "http://www.example.com/about_us" instead of "http://localhost:3000/about_us". This causes email specs to fail where we check if the url has been added to an email. |
For emails you also have to configure |
That is configured. This issue is not related to email. While running any spec if I debug and check what urls are generated with 'xyz_url' method, it always prepends it with with 'http://www.example.com/'. It should ideally prepend whatever value is set to Rails.application.default_url_options[:host] in test environment. |
RSpec doesn't have any value for this url, it comes from Capybara or from your config. |
While debugging the spec if I check value of
And when we check value of
it gives
So as you said, I think, the value is being set by either capybara or may be Rspec. Any thoughts? |
That'll be the value from the Rails helpers we mix in, you need to check |
Hey @JonRowe I checked and found that issue is exactly where @beydogan is suggesting in the first comment.
is always nil, no matter where I set the default_url_options. As of now in my application I have set the default host at all possible places. config/environments/test.rb
spec/support/capybara.rb
|
Any further comments? |
Sample app here: https://github.com/samphippen/demo_app_for_1275.git Worth noting: this explicitly is not supposed to work in controller specs according to someone from rails core. So request specs work, and feature specs don't work here. |
For future reference: I tried all sorts of variations (e.g |
I tried everything described in this issue too and my tests still broken. I'm using apartment with custom domains and it's necessary setup default_url_options with custom domain. |
anything found and tried, always end up with an error. prefixing for now with kind of the following |
+expected_url = foo_bars_url(foo)
get url_for(foo)
expect(json).to include(
"links" => a_hash_including(
- "bars" => foo_bars_url(foo)
+ "bars" => expected_url
)
) UPDATE: I'm no longer seeing this behaviour. It could have been an error on my part. |
I'm seeing the exact same situation as @rvsingh. Any attempt to configure default_url_options is effectively ignored. I have been able to work around the issue by acquiescing, and re-configuring default_url_options to use the host "www.example.com". Thanks to @kurko for the suggestion. This is incredibly frustrating because rspec-rails is effectively forcing you to use "www.example.com". If you have any other desire, you're left with options that feel wrong. |
I'm sorry you're frustrated, but we just set a default value if one isn't present, it's up to Rails how it uses that value, I suspect its being cached in a way making it difficult to override. |
For controller tests inside the config.action_controller.default_url_options = {
host: ENV['HOST']
} |
Same issue. Alternate resolution. I noticed that a random feature test was failing inconsistently in both dev machine and CI environments. After reproducing with The interesting thing was that only the first feature test would fail. For all the subsequent feature tests in any feature testing file, the URL helpers would still resolve incorrectly, but the visit would be successful. In other words, visiting www.example.com/my_page would render my page correctly and run the necessary assertions as intended. Simplest hack possible for me was just visiting one page before running the real tests:
Added this to the RSpec configure block in my rails helper and haven't had any problems since. ruby 2.3.4p301 |
These options are ignored on system tests. def visit_to(path)
visit("http://test.lvh.me"+":#{Capybara.server_port}"+path)
end |
I think this is just a Rails issue. There are many places to set In Rails 5.1.4, I have tested the following scenarios on web and console (but not in Rspec):
|
any one fix this problem? |
Regardless of the last comments. I think we can close this issue. Feel free to comment if we think we should still look at it. |
setting domains and protocols site wide was harder to tweak as first thought since Capybara’s feature tests do not respect the universal protocol setting of HTTPS, which our views and use of _url start to create. This led me back to this issue which reminds us why we have the domain configured at the bottom and that we might need to do soemthing similar for protocol.. rspec/rspec-rails#1275
setting domains and protocols site wide was harder to tweak as first thought since Capybara’s feature tests do not respect the universal protocol setting of HTTPS, which our views and use of _url start to create. This led me back to this issue which reminds us why we have the domain configured at the bottom and that we might need to do soemthing similar for protocol.. rspec/rspec-rails#1275
Any fixes for this problem ? |
I faced with the same issue. I found out that |
If you set
This fixes the issue originally described. |
In my rspec_helper.rb I defined;
It works fine for non-feature specs but doesn't work for feature specs. Host for urls are equal to "www.example.com" in feature specs.
I've debugged and in feature_example_group.rb file, default_url_options is always an empty hash.
https://github.com/rspec/rspec-rails/blob/master/lib/rspec/rails/example/feature_example_group.rb#L18
found the following solution for this issue;
beydogan@b590c82
Is this valid?
The text was updated successfully, but these errors were encountered: