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 7558121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:17:36+00:00 2026-05-30T12:17:36+00:00

I want to stub out the space_available_mb method in SubmissionsController so that it returns

  • 0

I want to stub out the space_available_mb method in SubmissionsController so that it returns 5. This doesn’t work. It returns the correct space on the real hard disk.

if space_available_mb is commented out, an expectation error is thrown, which means should_receive works correctly. However, it doesn’t return 5, but the real number, which means and_return fails for some reason.

Further debugging reveals that and_return is actually called, but only after the method has run and returned the real number.

Scenario: Hard Disk Space is low on new submission
  Given I am on the new_submission page
  And hard disk space is low
  Then I should see "Low disk space!"

Given /^hard disk space is low$/ do
  SubmissionsController.should_receive(:space_available_mb).and_return(5)
end

class SubmissionsController < ApplicationController
  include FileManager

  def new
    space = space_available_mb
    ...
  end
end

module FileManager
  def space_available_mb
    ...
  end
end
  • 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-05-30T12:17:37+00:00Added an answer on May 30, 2026 at 12:17 pm

    Depending on a mock framework, the syntax differs, but what you’re trying to accomplish is stubbing a method on any instance of your controller. In RSpec, you have this. So according to your attempts:

    Given /^hard disk space is low$/ do
      SubmissionsController.any_instance.should_receive(:space_available_mb).and_return(5)
    end
    

    If you plan to use any other mock frameworks, there’s bound to be a method like this any_instance of RSpec’s.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to find out that the screen is OFF when user locked a
I want to mock/stub: @the_bill = GovKit::OpenCongress::Bill.find_by_idents(112-s368).first for use in my tests. which returns
I have a class with a private set property that I want to stub
OK,I know that I should use a DOM parser, but this is to stub
I want to stub out a logged in user (with Devise/Warden) using rspec mocks
I want to use stub entities but it seems I cannot use TryGetObjectStateEntry without
Want my FireFox at work to be in sync with my FireFox at my
I want to call haskell functions out of C++ and did use the tutorial
I am currently struggling with stubbing out a helper method of my Sinatra app
I'm trying to mock a method that has the equivalent of the following signature:

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.