可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I have a rails application which acts differently depending on what domain it's accessed at (for example www.myapp.com will invoke differently to user.myapp.com). In production use this all works fine but my test code always sees a hostname of "www.example.com".
Is there a clean way of having a test specify the hostname it's pretending to access?
回答1:
@request.host = 'user.myapp.com'
回答2:
I think all answers are incomplete here... To enumerate all possible cases:
Integration Specs (inheriting from ActionDispatch::IntegrationTest
):
host! "my.awesome.host"
See the docs, section 5.1 Helpers Available for Integration Tests.
Controller Specs (inheriting from ActionController::TestCase
)
@request.host = 'my.awesome.host'
See the docs, section 4.4 Instance Variables Available.
Feature Specs (through Capybara)
Capybara.default_host = "http://my.awesome.host" # Or to configure domain for route helpers: default_url_options[:host] = "my.awesome.host"
From @AminAriana's answer
View Specs (inheriting from ActionView::TestCase
)
@request.host = 'my.awesome.host'
...or through RSpec:
controller.request.host = "my.awesome.host"
See the rspec-rails
view spec docs.
回答3:
In Feature specs, host! has been deprecated. Add these to your rspec_helper.rb:
# Configure Capybara expected host Capybara.app_host = # Configure actual routes host during test before(:each) do default_url_options[:host] = end
回答4:
I believe you can modify the HTTP_HOST
or SERVER_NAME
environment vars to change the request that goes to the router:
ENV['SERVER_NAME'] = "user.myapp.com"
See raw_host_with_port
in actionpack/lib/action_controller/request.rb.
回答5:
Another thing to remember is to make sure to use the correct session instance so that you can properly encapsulate the url helpers.
Integration tests provide you with a default session. You can call all session methods directly from your tests
test "should integrate well" do https! get users_path assert_response :success end
All these helpers are using the default session instance, which if not changed, goes to "www.example.com". As has been mentioned the host can be changed by doing host!("my.new.host")
If you create multiple sessions using the open_session method, you must ALWAYS use that instance to call the helper methods. This will properly encapsulate the request. Otherwise rails will call the default session instance which may use a different host:
test "should integrate well" do sess = open_session sess.host! "my.awesome.host" sess.get users_url #=> WRONG! will use default session object to build url. sess.get sess.users_url #=> Correctly invoking url writer from my custom session with new host. sess.assert_response :success end
If you intended to use the default session object, then you'll have to alter that host as well:
test "should integrate well" do sess = open_session sess.host! "my.awesome.host" host! sess.host #=> Set default session host to my custom session host. sess.get users_url end
回答6:
@request.host = 'user.myapp.com'
is not right. should use host!('user.myapp.com')
回答7:
I tried many variations of @request.host
, host!
, and post path, args, {'SERVER_NAME' => my_secret_domain}
without success, both as controller tests and feature tests. Very aggravating, as so many others reported success with those approaches.
The solution for me was:
request.headers["SERVER_NAME"] = my_secret_domain post path, args
I'm running ruby 2.1.5p273, rspec 3.1.7 and Rails 4.2.0
回答8:
Yet another answer:
request.host = "user.myapp.com"
I know it resembles the correct answer, but please bare with me. I don't like assignment operation in test just to set things up, I'd prefer an explicit stub. Interestingly, stubbing like this won't work:
allow(request).to receive(:host).and_return("user.myapp.com")
I personally prefer stubbing over assignment, that way I get 2 benefit, one is that it will be validated by rspec's verify double, second is that it is explicitly saying that is a stub, not part of the test excercise.
回答9:
None of the ways suggested in other answers at the point worked for me. This worked:
Capybara.configure { |config| config.default_host = "my.domain.com" }