Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rail3/rspec/devise: rspec controller test fails unless I add a dummy=subject.current_user.inspect

I'm trying to get RSpec working for a simple scaffolded app, starting with the rspec scaffold tests.

Per the devise wiki, I have added the various devise config entries, a factory for a user and an admin, and the first things I do in my spec controller is login_admin.

Weirdest thing, though... all my specs fail UNLESS I add the following statement right after the it ... do line:

dummy=subject.current_user.inspect

(With the line, as shown below, the specs pass. Without that line, all tests fail with the assigns being nil instead of the expected value. I only happened to discover that when I was putting some puts statements to see if the current_user was being set correctly.)

So what it acts like is that dummy statement somehow 'forces' the current_user to be loaded or refreshed or recognized.

Can anyone explain what's going on, and what I should be doing differently so I don't need the dummy statement?

#specs/controllers/brokers_controller_spec.rb
describe BrokersController do
  login_admin

  def valid_attributes
    {:name => "Bill", :email => "[email protected]", :company => "Example Inc", :community_id => 1}
  end

  def valid_session
    {}
  end

  describe "GET index" do
    it "assigns all brokers as @brokers" do
      dummy=subject.current_user.inspect    # ALL SPECS FAIL WITHOUT THIS LINE!
      broker = Broker.create! valid_attributes
      get :index, {}, valid_session
      assigns(:brokers).should eq([broker])
    end
  end
  describe "GET show" do
    it "assigns the requested broker as @broker" do
      dummy=subject.current_user.inspect  # ALL SPECS FAIL WITHOUT THIS LINE!
      broker = Broker.create! valid_attributes
      get :show, {:id => broker.to_param}, valid_session
      assigns(:broker).should eq(broker)
    end
  end

and per the devise wiki here is how I login a :user or :admin

#spec/support/controller_macros.rb
module ControllerMacros
  def login_admin
    before(:each) do
      @request.env["devise.mapping"] = Devise.mappings[:admin]
      sign_in Factory.create(:admin) # Using factory girl as an example
    end
  end

  def login_user
    before(:each) do
      @request.env["devise.mapping"] = Devise.mappings[:user]
      user = Factory.create(:user)
      user.confirm! # or set a confirmed_at inside the factory. Only necessary if you are using the confirmable module
      sign_in user
    end
  end
end
like image 360
jpw Avatar asked Mar 01 '12 00:03

jpw


2 Answers

What a struggle! Thank you Robin, I've been googling on this for hours and finally saw your post; now my controller tests are working :)

To add to your answer, I figured out how to get the devise session into the valid_session hash, which allows the controller tests to run properly as generated by rails.

def valid_session
  {"warden.user.user.key" => session["warden.user.user.key"]}
end
like image 67
Mark Smith Avatar answered Oct 07 '22 01:10

Mark Smith


In your tests, there is the following code:

 def valid_session
    {}
 end
 ...
   get :index, {}, valid_session

Because of this 'session' variable, the "log_in" that you did is essentially not being used during the 'get'.

The way that I solved it was to remove all of the "valid_session" arguments to the get, post, put, delete calls in that controller's spec. The example above becomes:

get :index, {}

I suspect that there's a way to add the devise's session to the "valid_session" hash, but I don't know what it is.

like image 23
RobinD Avatar answered Oct 07 '22 01:10

RobinD