Simple controller test failing

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])      
    it "renders the index template" do     
      get :index 
      expect(response).to render_template('index')     



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


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 ==)

@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.

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

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.

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

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] =
  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 =>
 session[:manager_id] =
 redirect_to "/manage"

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