user1596316 user1596316 - 1 year ago 56
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 =
@thebrands = Brand.all
render "categories/new"

In my test

require 'rails_helper'

RSpec.describe CategoriesController, type: :controller do

before(:all) do
@the_super_admin = createLoggedInSuperAdmin

context "#new" do

it "instantiates new category" do
get :new

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

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 Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download