Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RSpec 2.3 + Devise 1.0.11

I've got a really old Rails 2.3.18, ruby 1.9.3, rspec 1.x application which we are upgrading and it had restful-authentication in it. So I've replaced that with Devise 1.0.11.

I can login to the application, but my tests will not run;

Here is the test in question

require 'spec_helper'

describe CategoriesController do
  context "As a logged in user" do
    before do
      login_user
      current_firm = mock_model(Firm, :id => 1)
      controller.stub!(:current_firm).and_return(current_firm)
    end

    describe "#index" do
      it "should render index" do
        get :index
        response.should render_template('index')
      end
    end

  end
end

Here is the error I get;

NoMethodError in 'CategoriesController As a logged in user#index should render index'
You have a nil object when you didn't expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.[]=
/home/map7/code/pdfcat/spec/spec_helper.rb:18:in `login_user'
spec/controllers/categories_controller_spec.rb:6:in `block (3 levels) in <top (required)>'

The error happens on this line;

[20, 29] in /usr/local/rbenv/versions/1.9.3-p551/lib/ruby/gems/1.9.1/gems/warden-0.10.7/lib/warden/session_serializer.rb
   20        key
   21      end
   22  
   23      def store(user, scope)
   24        return unless user
=> 25        session[key_for(scope)] = serialize(user)
   26      end

The problem is 'session' is nil when I'm at this point.

I've pushed the full code to here: https://github.com/map7/pdfcat/tree/devise

My plan was to get devise working in the tests then I could jump to Rails 3.0 and continue the upgrade.

like image 923
map7 Avatar asked Jun 29 '17 06:06

map7


1 Answers

There's an old message in google groups that I think is relevant: https://groups.google.com/forum/#!topic/rspec/4AHuPtHFD34

It recommends using this:

before do
  request.env['warden'].stub(:authenticate!) { double(User) }
end

I'd probably put it in rails_helper.rb so that it runs for all tests

like image 53
jemminger Avatar answered Sep 18 '22 13:09

jemminger