Rails: invalid host name for rsspec url helpers

Url helpers (e.g. root_url) return a different hostname in application controllers against rspec examples. I was able to successfully set the domain for URL Helpers in my rails application as follows:

class ApplicationController < ActionController::Base def default_url_options {host: "foo.com", only_path: false} end end 

For example, root_url displays "http://foo.com/" in the application, but "http://example.com" in my query specifications. What is the recommended way to apply these URL parameters globally in rails 3.2 so that they affect my specifications? URL parameters do not have to be dynamic.

+6
source share
4 answers

In the specifications of the features, host! outdated. Add them to your rspec_helper.rb:

 # Configure Capybara expected host Capybara.app_host = 'http://lvh.me:3000' # Configure actual routes host during test before(:each) do if respond_to?(:default_url_options) default_url_options[:host] = 'http://lvh.me:3000' end end 
+4
source

Just to add an answer to Dan, it will look something like this in spec_helper.rb

 RSpec.configure do |config| # other config options config.before(:each) do host! "localhost:3000" end end 
+4
source

You can set the host in front of the block in your test in rspec:

  before :each do host! 'hostgoeshere.com' end 

If you want to install it globally, you can put something in your spec_helper.rb file.

+2
source

I tried a lot of options @request.host , host! and post path, args, {'SERVER_NAME' => my_secret_domain} without success, both in controller tests and in function tests. Very aggravating, as many others have reported success with these approaches.

The solution for me was:

 request.headers["SERVER_NAME"] = my_secret_domain post path, args 

I am running ruby ​​2.1.5p273, rspec 3.1.7 and Rails 4.2.0

0
source

Source: https://habr.com/ru/post/915308/


All Articles