← Back to Upcase

Simple controller test failing


(Jon Seidel) #1

I just got an existing application to support (unfortunately no tests at all) and I’m in the processing of specing out a new feature they want. Everything’s going along fine: I created the controller and the model and started my first tests on the controller but have run into what I guess is a head-slapper of a problem that I can’f figure out:

Controller spec

require 'spec_helper'    
describe Manage::FeaturedCompaniesController do   
  describe "GET index" do  
    it "assigns @featured_companies" do     
      featured_company = FeaturedCompany.create   
      get :index     
      expect(assigns(:featured_companies)).to eq([featured_company])      
    end   
                                                                                                                                                              
    it "renders the index template" do     
      get :index 
      expect(response).to render_template('index')     
    end         
  end     

end

Controller

class Manage::FeaturedCompaniesController < Manage::BaseController                                                                                 
  def index                                                         
    @featured_companies = FeaturedCompany.find(:all)
 end                                                                                                             

end

Error Message

 1) Manage::FeaturedCompaniesController GET index assigns @featured_companies
     Failure/Error: expect(assigns(:featured_companies)).to eq([featured_company])
       
       expected: [#<FeaturedCompany id: 1>]
            got: nil
       
       (compared using ==)

(Luís Ferreira) #2

@JESii I created a new application, with the controller and spec you provided, and the tests pass. There must be something else wrong with it.


(Derek Prior) #3

Is there authentication (inherited from your base controller, perhaps?) in the way here? It sounds as if your assignment isn’t happening.


(Jon Seidel) #4

Thanks @derekprior… I should have thought of that - you were right! Now off to figure out how to simulate the login for this and future specs.


(Luís Ferreira) #5

@JESii Usually there we’ll be some kind of backdoor for testing.


(Jon Seidel) #6

Thanks, @zamith… I was able to get it working with the help of this post, but I’m not sure I’d call it a ‘backdoor’? I’ve seen that in the various user authentication gems such as devise or monban but this app uses ActiveModel::SecurePasswords and pretty much rolls its own.

I do have a question about the implementation, though. In the production code, the user id is authenticated and then there’s a redirect_to…

in def create...
manager = Manager.where(:email => params[:email]).first
if manager && manager.authenticate(params[:password])
  session[:manager_id] = manager.id
  redirect_to "/manage", :notice => "Logged in!"

However, that :notice item caused my simulated login to fail…and I wound up coding it like so:

def login_manager(user)
  manager = Manager.where(:email => user.email).first
 session[:manager_id] = manager.id
 redirect_to "/manage"
end

I don’t need that flash in testing, but I’d like to know why it doesn’t work.