Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8586117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:13:58+00:00 2026-06-11T22:13:58+00:00

I have a helper that accesses request.fullpath . Within an isolated helper test, request

  • 0

I have a helper that accesses request.fullpath. Within an isolated helper test, request is not available. What should I do? Can I somehow mock it or something like that?

I’m using the newest versions of Rails and RSpec. Here’s what my helper looks like:

def item(*args, &block)
  # some code

  if request.fullpath == 'some-path'
    # do some stuff
  end
end

So the problematic code line is #4 where the helper needs access to the request object which isn’t available in the helper spec.

Thanks a lot for help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T22:14:00+00:00Added an answer on June 11, 2026 at 10:14 pm

    Yes, you can mock the request. I had a whole long answer here describing how to do that, but in fact that’s not necessarily what you want.

    Just call your helper method on the helper object in your example. Like so:

    describe "#item" do
      it "does whatever" do
        helper.item.should ...
      end
    end
    

    That will give you access to a test request object. If you need to specify a specific value for the request path, you can do so like this:

    before :each do
      helper.request.path = 'some-path'
    end
    

    Actually, for completeness, let me include my original answer, since depending on what you’re trying to do it might still be helpful.

    Here’s how you can mock the request:

    request = mock('request')
    controller.stub(:request).and_return request
    

    You can add stub methods to the returned request similarly

    request.stub(:method).and_return return_value
    

    And alternative syntax to mock & stub all in one line:

    request = mock('request', :method => return_value)
    

    Rspec will complain if your mock receives messages that you didn’t stub. If there’s other stuff Just call your request helper method on the helper object is doing that you don’t care about in your test, you can shut rspec up by making the mock a “null object”,example. like Like so

     request = mock('request').as_null_object
    

    It looks like all you probably need to get your specific test passing is this:

    describe "#item" do
      let(:request){ mock('request', :fullpath => 'some-path') }
    
      before :each do
        controller.stub(:request).and_return request
      end
    
      it "does whatever"
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a helper class that should only ever run in a background thread.
I have a custom helper that i use for logging. Within one of the
I have a helper class that I need context so I can access the
I have this helper method that I can't seem to clean up with content_tag
I have a helper that checks if a user is signed in or not:
I have a helper class that I want to display a UIAlertView then will
I have an Html Helper that converts an Enum into a SelectList like so:
I have a helper class that is passed an array of values that is
I have a view helper that manages generating thumbnails for images. The images are
I have a markdown helper that looks like this in application_helper.rb . How would

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.