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

  • SEARCH
  • Home
  • 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 8960033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:34:53+00:00 2026-06-15T15:34:53+00:00

Working in Sinatra, a local object request is created and made available to all

  • 0

Working in Sinatra, a local object request is created and made available to all views and helpers. So, I can make an ApplicationHelper module with helper methods, and if the helper methods are called in the view they can in turn call the request object, like so:

module ApplicationHelper
  def nav_link_to(text,path)
    path == request.path_info ? klass = 'class="current"' : klass = ''
    %Q|<a href="#{path}" #{klass}>#{text}</a>|
  end
end

Now, I want to test this, but in my test the request object doesn’t exist. I tried to mock it, but that didn’t work. Here’s my test so far:

require 'minitest_helper'
require 'helpers/application_helper'

describe ApplicationHelper do

  before :all do
    @helper = Object.new
    @helper.extend(ApplicationHelper)
  end

  describe "nav links" do
    before :each do
      request = MiniTest::Mock.new
      request.expect :path_info, '/'
    end

    it "should return a link to a path" do
      @helper.nav_link_to('test','/test').must_equal '<a href="/test">test</a>'
    end

    it "should return an anchor link to the current path with class 'current'" do
      @helper.nav_link_to('test','/').must_equal '<a href="test" class="current">test</a>'
    end
  end
end

So, how can you mock a ‘local’ object so that the code your testing can call it?

  • 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-15T15:34:54+00:00Added an answer on June 15, 2026 at 3:34 pm

    You need to make sure there is a request method on your @helper object that returns the mock request object.

    In RSpec I’d just stub it. I’m not particularly familiar with Minitest, but a quick look suggests that this might work in recent versions (if you change request to @request in your before :each):

    it "should return a link to a path" do
      @helper.stub :request, @request do
        @helper.nav_link_to('test','/test').must_equal '<a href="/test">test</a>'
      end
    end
    

    Update

    Since Minitest requires that the stubbed method is already defined on the object, you could make @helper an instance of Struct.new(:request) instead of Object, i.e.

    @helper = Struct.new(:request).new
    

    And actually, having done that, you might not need the stub at all! You could just do

    before :each do
      @helper.request = MiniTest::Mock.new
      @helper.request.expect :path_info, '/'
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on my first Sinatra/CouchDB project and I'm getting an error I can't
Greetings, Every time I make a change to a Sinatra app I'm working on
I'm working in a Sinatra application using Sequel. I want to make a transaction,
My Sinatra app was working on my local server a few days ago, but
I made a simple Sinatra app to run a homepage. It's already working on
It's the first time I'm working with Sinatra and I just can't get sessions
I hope you can help me. I am working on a small Sinatra Rails
I am working on a simple app in Sinatra with DataMapper. I want to
I am working on a simple budget app using Sinatra and DataMapper in Ruby.
Working with an undisclosed API, I found a function that can set the number

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.