I have such a simple test.

require 'rails_helper' RSpec.describe TasksController, type: :controller do describe "GET #index" do it "returns a success response" do get :index expect(response).to have_http_status(:ok) end end end 

To such a controller.

 class TasksController < ApplicationController expose :task, -> {current_user.tasks.find(params[:id])} expose :active_tasks, -> {current_user.tasks.where(active: false).order('priority ASC')} expose :completed_tasks, -> {current_user.tasks.where(active: true).order('priority ASC')} def index render json: {active: active_tasks, completed: completed_tasks}, status: 200, each_serializer: TasksSerializer end def show render json: task, status: 200, each_serializer: TaskSerializer end def create task = current_user.tasks.create(task_params) render json: task.id, status: 201, each_serializer: TaskSerializer end def update if task.update(task_params) render json: task, status: 200 else render json: task.errors, status: 422, each_serializer: TaskSerializer end end def destroy if task.destroy return head(:ok) else return head(:bad_request) end end def batch_destroy tasks = current_user.tasks.where(id: params[:ids]).destroy_all end private def task_params params.require(:task).permit(:title, :description, :priority, :due_date, :active) end end 

After the rspec command in the console, this error crashes when passing the test:

 Failures: 1) TasksController GET #index returns a success response Failure/Error: expose :active_tasks, -> {current_user.tasks.where(active: false).order('priority ASC')} NoMethodError: undefined method `tasks' for nil:NilClass # ./app/controllers/tasks_controller.rb:3:in `block in <class:TasksController>' # /home/milestep/.rvm/gems/ruby-2.5.0/gems/decent_exposure-3.0.2/lib/decent_exposure/flow.rb:67:in `instance_exec' # /home/milestep/.rvm/gems/ruby-2.5.0/gems/decent_exposure-3.0.2/lib/decent_exposure/flow.rb:67:in `handle_options_override' # /home/milestep/.rvm/gems/ruby-2.5.0/gems/decent_exposure-3.0.2/lib/decent_exposure/flow.rb:55:in `block in handle_flow_method' # /home/milestep/.rvm/gems/ruby-2.5.0/gems/decent_exposure-3.0.2/lib/decent_exposure/flow.rb:85:in `fetch_ivar' # /home/milestep/.rvm/gems/ruby-2.5.0/gems/decent_exposure-3.0.2/lib/decent_exposure/flow.rb:53:in `handle_flow_method' # /home/milestep/.rvm/gems/ruby-2.5.0/gems/decent_exposure-3.0.2/lib/decent_exposure/flow.rb:25:in `method_missing' # /home/milestep/.rvm/gems/ruby-2.5.0/gems/decent_exposure-3.0.2/lib/decent_exposure/exposure.rb:181:in `block in attribute' # /home/milestep/.rvm/gems/ruby-2.5.0/gems/decent_exposure-3.0.2/lib/decent_exposure/context.rb:58:in `instance_exec' # /home/milestep/.rvm/gems/ruby-2.5.0/gems/decent_exposure-3.0.2/lib/decent_exposure/context.rb:58:in `fetch_value' # /home/milestep/.rvm/gems/ruby-2.5.0/gems/decent_exposure-3.0.2/lib/decent_exposure/context.rb:23:in `get' # /home/milestep/.rvm/gems/ruby-2.5.0/gems/decent_exposure-3.0.2/lib/decent_exposure/attribute.rb:46:in `block (2 levels) in expose!' # ./app/controllers/tasks_controller.rb:7:in `index' # /home/milestep/.rvm/gems/ruby-2.5.0/gems/rails-controller-testing-1.0.2/lib/rails/controller/testing/template_assertions.rb:61:in `process' # /home/milestep/.rvm/gems/ruby-2.5.0/gems/rails-controller-testing-1.0.2/lib/rails/controller/testing/integration.rb:12:in `block (2 levels) in <module:Integration>' # ./spec/controllers/tasks_controller_spec.rb:5:in `block (3 levels) in <top (required)>' Finished in 0.28294 seconds (files took 1.54 seconds to load) 10 examples, 1 failure Failed examples: rspec ./spec/controllers/tasks_controller_spec.rb:4 # TasksController GET #index returns a success response 

Tell me what you need to change to pass the test, I will be very grateful, thank you.

    2 answers 2

    You must do the following:

    1) Create a helper file to test devise controllers with the following content

     # spec/support/controller_helper.rb RSpec.configure do |config| config.include Devise::Test::ControllerHelpers, type: :controller end 

    2) Uncomment a single line in spec/rails_helper.rb

     Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f } 

    3) Add one line to your controller test

     before { sign_in User.create(email: 'foo@example.com', password: '12345678') } 

    As a result of the above actions, before running the controller tests, authorization will be performed and, as a result, the current_user method will not return nil

      The error text speaks for itself:

       Failure/Error: expose :active_tasks, -> {current_user.tasks.where(active: false).order('priority ASC')} NoMethodError: undefined method `tasks' for nil:NilClass 

      Your controller action assumes a logged in user (current_user), and in your test it is not. To test passed, it is necessary to create a user and log it. How to do it depends on what you use, factories or fixtures, and what you use to authenticate users. If devise, then he has a special sign_in(user) helper for the tests.

      Well, in general, the controller should not fall with an error in this case, but redirect to the login page. Devise can do it again