Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
983 views
in Technique[技术] by (71.8m points)

rspec - How do I change the default "www.example.com" domain for testing in rails?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...