rail3/rspec/devise: rspec controller test fails un

2019-02-12 17:27发布

问题:

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 => "rspec_broker@example.com", :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

回答1:

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


回答2:

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.



回答3:

Thanks for this solution.

If you are using a different Devise model, the session id also changes.

For a model Administrator use the following:

def valid_session
    {'warden.user.administrator.key' => session['warden.user.administrator.key']}
end