2

So I was just trying to call a log_in method from user controller in the RSpec as

it "should get the index page" do @user = User.new({ :email => "[email protected]" }) log_in(@user) get 'index' response.should be_success end 

The result I got is like

1) EmployeesController GET 'index' should get the index page Failure/Error: log_in(user) NoMethodError: undefined method `log_in' for #<RSpec::Core::ExampleGroup::Nested_1:0x4ac0328> # ./spec/controllers/employees_controller_spec.rb:11:in `user_log_in' # ./spec/controllers/employees_controller_spec.rb:16:in `block (2 levels) in <top (required)>' 

Can someone help me out? Thanks

Edited March 11th, 2011

Here is the log_in method which is in UserController

 def log_in(user) session[:current_user] = user.id end 
6
  • Any chance you are using Devise? Commented Mar 5, 2011 at 7:23
  • @raidfive No, I didn't use Devise Commented Mar 7, 2011 at 17:59
  • Is the method user_log_in or log_in? Can you include this method? Commented Mar 7, 2011 at 19:05
  • @raidfive Sorry for the update delay, here it is Commented Mar 12, 2011 at 0:32
  • So, you are trying to call the log_in method in the UserController from the EmployeesController? This isn't really possible. Commented Mar 12, 2011 at 0:59

2 Answers 2

14

If you want to call a method on the controller in an RSpec controller test, you could use the following.

subject.send(:log_in,@user) 

It should call the method. I dont know if this is really a best practice. A better method would be to stub the logged_in method as BurmajaM suggested.

Sign up to request clarification or add additional context in comments.

1 Comment

what is subject? I know I can do controller.send, so I'm assuming this is like User.send(:log_in, @user)?
5

Why don't you stub logged_in? or whatever your method is. Logging in is not target of this spec, so stub it! Here's simple example how I spec controller action that has before_filter:

class MyController < ApplicationController before_filter :logged_in? def index end end describe MyController do describe "GET 'index'" do context "when not logged in" # you want to be sure that before_filter is executed it "requires authentication" do controller.expects :logged_in? get 'index' end # you don't want to spec that it will redirect you to login_path # because that spec belongs to #logged_in? method specs end context "when authenticated" do before(:each) { controller.stubs :logged_in? } it "renders :index template" do get 'index' should render_template(:index) end it "spec other things your action does when user is logged in" end end end 

5 Comments

I did try use stub, but it seems like it doesn't work if I'm in another controller spec file. In this case, I'm in EmployeeController spec and calling UserController
Hmmm ... that's weird! Why are you calling UserController in EmployeeController spec? It doesn't have to do anything with #logged_in? right?
@BurmajaM There is a authentication check as before_filter for the EmployeeController, which I thought I need to log in to pass this before_filter
I thought so, that's why I told you to stub it because it is not target of this spec. Before filters are just simple methods that are executed before controller action, thus they can be stubbed like: before(:each) { controller.stubs :logged_in? }
Thanks again, maybe I did something wrong, I will dig into it

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.