user1596316 user1596316 - 2 months ago 7
Ruby Question

rspec - Newly instantiated record is nil according to assigns

In my controller:

class CategoriesController < ApplicationController
before_filter :authenticate_super_admin!

def new
@thecategory = Category.new
@thebrands = Brand.all
render "categories/new"
end
end


In my test

require 'rails_helper'

RSpec.describe CategoriesController, type: :controller do

before(:all) do
@the_super_admin = createLoggedInSuperAdmin
end

context "#new" do

it "instantiates new category" do
get :new

expect(assigns(:thecategory)).to be_a_new(Category)
end
end
end


it keeps on telling me
expected nil to be a kind of Model
and when I inspect it with pry assigns(:themodel) shows as nil

I haven't been able to find any answers that help me with this situation

Answer

Turns out I was an idiot, the createLoggedInSuperAdmin call should have been wrapped in before(:each) instead of before(:all), that was why the authentication was working for the first test but failing on the second one