Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

undefined method `server_name' for #<ActionController::TestRequest

#1572

Hi, while starting to setup functional tests for my application I received the following error:

NoMethodError: undefined method server_name' for

<ActionController::TestRequest

which is coming from an instance variable I'm setting up in my application controller.

@domain = Domain.find_or_create_by_name(request.server_name)

I'm using rails 2.0.2, any guidance would be appreciated.

Reported by Patrick · December 14th, 2008 @ 06:20 PM

State: invalid
Milestone: 2.x
Assigned to: Frederick Cheung Frederick Cheung
Importance: none

Activity

  1. Patrick
    Patrick
    • Assigned user set to Frederick Cheung

    December 15th, 2008 @ 07:46 PM

  2. Roger
    Roger

    I assume that this is for a "related" database [connected] and should work [and normally does?]

    December 16th, 2008 @ 09:20 PM

  3. Frederick Cheung
    Frederick Cheung

    the server_name method just isn't defined on the fake requests that passed in. It should be hard to add this, similar to how user_agent or host are set (in test_process.rb).

    Alternatively you could always stick

    
    def setup
      def @request.server_name
        "foo.bar"
      end
    end
    

    in the relevant test cases

    December 16th, 2008 @ 11:58 PM

  4. Patrick
    Patrick

    Thanks for getting back to me, I appreciate it.

    Yes, it works fine outside the normal "testing" context.

    Regarding Fredricks's response. I thought I tried that inside my functional test, but still received the error. Let me try it again though.

    December 17th, 2008 @ 03:53 PM

  5. Patrick
    Patrick

    The other thing is that this test is relevant in all my test cases, but let me try the work around out again.

    December 17th, 2008 @ 04:54 PM

  6. Patrick
    Patrick

    I still get the same error:

    NoMethodError: undefined method server_name' for <ActionController::TestRequest

    when defining

    def setup def @request.server_name

    "127.0.0.1"
    
    

    end end

    in the test case specifically.

    December 18th, 2008 @ 03:17 PM

  7. Patrick
    Patrick

    Any other recommendations for this one?

    January 13th, 2009 @ 02:12 AM

  8. Pratik
    Pratik
    • State changed from new to incomplete

    Is this still an issue on edge ? A failing test case would be great ! http://guides.rails.info/contrib... should be helpful.

    Thanks

    March 13th, 2009 @ 11:01 AM

  9. Ryan Bigg
    Ryan Bigg
    • State changed from incomplete to invalid

    This is working on 2.3.5 and latest Rails for me.

    April 11th, 2010 @ 10:38 PM