I am needing to stub the response of a current_user
method in an Rspec/capybara request spec. The method is defined in ApplicationController
and is using helper_method. The method should simply return a user id. Within the test, I'd like this method to return the same user id each time.
Alternatively, I could fix my problem by setting session[:user_id]
in the spec (which is what current_user
returns)... but that doesn't seem to work either.
Are either of these possible?
Edit:
Here is what I've got (it is not working. It just runs the normal current_user method).
require 'spec_helper'
describe "Login" do
before(:each) do
ApplicationController.stub(:current_user).and_return(User.first)
end
it "logs in" do
visit '/'
page.should have_content("Hey there user!")
end
end
Also not working:
require 'spec_helper'
describe "Login" do
before(:each) do
@mock_controller = mock("ApplicationController")
@mock_controller.stub(:current_user).and_return(User.first)
end
it "logs in" do
visit '/'
page.should have_content("Hey there user!")
end
end
This works for me and gives me a
@current_user
variable to use in tests.I have a helper that looks like this:
And then in my request specs, I call:
For anyone else who happens to need to stub an application controller method that sets an ivar (and was stymied by endless wanking about why you shouldn't do that) here's a way that works, with the flavour of Rspec circa October 2013.
it stubs the method to do nothing, and sets the ivar on rspec's controller instance, and makes it available to the test as @campaign.
skalee seems to have provided the correct answer in the comment.
If the method you're trying to stub is an instance method (most likely) and not a class method then you need use:
ApplicationController.any_instance.stub(:current_user)
None of the provided responses worked for me. As in @matt-fordam's original post, I have a request spec, not a controller spec. The test just renders the view without launching a controller.
I resolved this by stubbing the method on the view as described in this other SO post
Here are a couple of examples of the basic form.
In your particular case, I believe the proper form would be:
Here's a full working example from a project I work on:
To explain my full example, basically what this does is verify that, when an
ActionController::InvalidAuthenticityToken
error is raised anywhere in the app, that a flash message appears, and the user is redirected to theportals_controller#index
action. You can use these forms to stub out and return specific values, test an instance of a given error being raised, etc. There are several.stub(:action_name).and_[do_something_interesting]()
methods available to you.Update (after you added your code): per my comment, change your code so it reads:
For Rspec 3+ the new api is:
For a controller test, nice and short:
Or for all instances of ApplicationController: