Topic: [RSpec] receiveing unexpected message [Self Proclaimed Solved!]

I'm trying to spec out the Sessions controller to the beast forum but running into some difficulty:

class SessionController < ApplicationController

  def create
    self.current_user = User.authenticate(params[:login], params[:password])
    if logged_in?
      if params[:remember_me] == "1"
        cookies[:login_token] = { :value => "#{current_user.id};#{current_user.reset_login_key!}", :expires => Time.now.utc+1.year}
      end
      redirect_to home_path
    else
      flash.now[:error] = "Invalid login or password, try again please."
      render :action => 'new'
    end
  end


describe SessionController, "handling POST /session" do

  before do
    @user = mock_user
    @session = mock_model(Session)
    @params = { :login => 'admin', :password => 'password' }
    User.stub!(:authenticate).and_return(@user)
  end

  def do_post
    post :create, @params
  end

  it "should use authenticate user" do
    @session.should_receive(:authenticate).with('admin','password').and_return(@user)
    do_post
  end
 
  it "should redirect to home path if create is successful"
  it "should render the new page if create fails"
end


My Error

2)
Spec::Mocks::MockExpectationError in 'SessionController handling POST /session should use authenticate user'
Mock 'User_1078' received unexpected message :last_seen_at with (no args)
/Volumes/EXTERNAL/web/omenking.ca/config/../lib/authentication_system.rb:52:in `current_user='
/Volumes/EXTERNAL/web/omenking.ca/config/../app/controllers/session_controller.rb:4:in `create'
./spec/controllers/session_controller_spec.rb:70:in `do_post'
./spec/controllers/session_controller_spec.rb:75:
script/spec:4:

This module is included in the application.rb
module AuthenticationSystem

    def current_user=(value)
      if @current_user = value
        session[:user_id] = @current_user.id
        # this is used while we're logged in to know which threads are new, etc
        session[:last_active] = @current_user.last_seen_at
        session[:topics] = session[:forums] = {}
        update_last_seen_at
      end
    end


    def current_user
      @current_user ||= ((session[:user_id] && User.find_by_id(session[:user_id])) || 0)
    end


I don't know why last_seen_at would be unexpected.

Last edited by OmenKing (2007-09-30 22:15:46)

Re: [RSpec] receiveing unexpected message [Self Proclaimed Solved!]

My expectation is wrong and should be the following:

  it "should use authenticate user" do
    User.should_receive(:authenticate).with('admin','password').and_return(@user)
    do_post
  end

But this doesn't appear to be my problem. I played around in the console and it appears that its having an issue with current_user.last_seen_at because current_user is infact nil.

If I remove the following from Session controller

self.current_user

Its passes!

So to solve it is a simple stub

controller.stub!(:current_user=)

Last edited by OmenKing (2007-09-30 22:15:15)

Re: [RSpec] receiveing unexpected message [Self Proclaimed Solved!]

Do you know why? Because "controller.current_user" is a protected, or private method! I think it ships that way with acts_as_authenticated.