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
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:
If you plan to use any other mock frameworks, there’s bound to be a method like this
any_instanceof RSpec’s.