How do I change the default “www.example.com” domain for testing in rails?

后端 未结 10 678
囚心锁ツ
囚心锁ツ 2020-11-30 04:17

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 produc

10条回答
  •  北荒
    北荒 (楼主)
    2020-11-30 05:06

    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 
    

提交回复
热议问题