vendredi 5 février 2016

When testing custom devise controller get an @controller is nil Runtime:Error

I had devise generate controllers for users with the console command "rails generate devise:controllers users", and when I try to run a simple test for registrations_controller.rb my console outputs the error: "RuntimeError: @controller is nil: make sure you set it in your test's setup method." I am new to devise, but everything has seemed pretty straightforward so I don't really see why this isn't working.

This is app/controllers/users/registrations_controller.rb :

class RegistrationsController < Devise::RegistrationsController

  # GET /resource/sign_up
  def new
    super
    @user = User.new
  end
end

This is config/routes.rb :

Rails.application.routes.draw do
  devise_for :users, controllers: { registrations: 'users/registrations'}
  devise_scope :users do
    root                'users/registrations#new'
    get "sign_up", to: "users/registrations#new"
  end
end

And this is test/controllers/registrations_controller_test.rb

require 'test_helper'


class RegistrationsControllerTest < ActionController::TestCase
include Devise::TestHelpers


    def setup 
            @user = users(:user1)
    end

    test "should get new" do
        get :new
        assert_response :success
    end
end

Aucun commentaire:

Enregistrer un commentaire