Topic: Authlogic sessions in RSpec tests

Hello,

I have started working on a blog using rails 3, authlogic and RSpec. All my tests were passing, but since I added a before_filter that requires a user to be logged in to access the blog administration, the admin blog tests needed to be updated.

I was able to get further, by reading this Adventures with Ruby Blog Post, but still couldn't get around the before filter I have placed. The code is good since it works in the browser, just need to get the RSpec tests working again.

Here is the code I currently have

app/controllers/application_controller.rb
added require_user, other methods taken from authlogic readme

class ApplicationController < ActionController::Base

  helper_method :current_user_session, :current_user

  def require_user
    redirect_to "/" if @current_user.blank?
  end


  private

  # below methods taken from authlogic's readme

  def current_user_session
    return @current_user_session if defined?(@current_user_session)
    @current_user_session = UserSession.find
  end

  def current_user
    return @current_user if defined?(@current_user)
    @current_user = current_user_session && current_user_session.user
  end

end

app/controllers/admin/blogs_controller.rb

class Admin::BlogsController < ApplicationController
  
  before_filter :require_user
  
  def index
    @blog_posts = BlogPost.find(:all)
  end
  
  # other crud methods below, just want to get index working first...
  
end

spec/spec_helper.rb
Methods from Adventures with Ruby Blog Post

def current_user(stubs = {})
  @current_user ||= mock_model(User, stubs)
end

def user_session(stubs = {}, user_stubs = {})
  @current_user_session ||= mock_model(UserSession, {:user => current_user(user_stubs)}.merge(stubs))
end

def login(session_stubs = {}, user_stubs = {})
  UserSession.stub!(:find).and_return(user_session(session_stubs, user_stubs))
end

def logout
  @user_session = nil
end


specs/controllers/admin/blog_controller_spec.rb

require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')

describe Admin::BlogsController do
  fixtures :blog_posts, :users
  
  def mock_blog_posts(stubs={})
    @mock_blog_posts ||= mock_model(BlogPost,stubs)
  end
  
  
  before(:each) do
    login
    @blog_posts = [mock_model(BlogPost)]
    BlogPost.stub!(:find).with(:all).and_return(@blog_posts)
  end
  
  
  describe "responding to GET index" do
    it "should grab all blog_posts" do
      BlogPost.should_receive(:find).with(:all).and_return([mock_blog_posts])
      get :index
      assigns["blog_posts"].should == [mock_blog_posts]
    end
  end

I get the following error when running specs

1) Admin::BlogsController responding to GET index should grab all blog_posts
    Failure/Error: BlogPost.should_receive(:find).with(:all).and_return([mock_blog_posts])
    (<BlogPost(id: integer, title: string, body: text, published: boolean, published_at: datetime, user_id: integer, created_at: datetime, updated_at: datetime) (class)>).find(:all)
        expected: 1 time
        received: 0 times
    # spec/controllers/admin/blogs_controller_spec.rb:37
    # /Library/Ruby/Gems/1.8/gems/activesupport-3.0.0.beta3/lib/active_support/dependencies.rb:209:in `inject'
    # spec/controllers/admin/blogs_controller_spec.rb:3

BlogPost.find is never called since the require_user before filter is redirecting the user to root. There could be something minor off with the login spec helper method, though i am unsure what could it be. Would I have to do a UserSession or User should_receive call before the BlogPost.should_receive one?