Rails Minitest test error

Asked 1 years ago, Updated 1 years ago, 97 views

The following error came out when I planned and spun the test as a mini test in Rail.

Minitest::UnexpectedError: AbstractController::DoubleRenderError: Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and at most once per action. Also note that neither redirect nor render terminate execution of the action, so if you want to exit an action after redirecting, you need to do something like "redirect_to(...) and return".

How should I resolve and test the above errors?

What I wanted to test was a simple alternator test as below.

class UsersControllerTest < ActionController::TestCase
test 'You can bring your self-introduction' do
    get :info, format: :js
    assert_response :success
  end
end
Actual controller
class UsersController < ApplicationController

def info

end
end

In real views other than tests
link_to('information', info_user_url, remote: true)

Put the link in the same way as above and click on the link, info.js.I can see the coffee view well. There is an error only in the test.

ruby-on-rails minitest testing

2022-09-21 15:27

1 Answers

See http://guides.rubyonrails.org/layouts_and_rendering.html#avoiding-double-render-errors.

I think an error occurs when the view is rendered more than once...

If the error occurs only in the test, refer to the following link. http://stackoverflow.com/questions/4291755/rspec-test-of-custom-devise-session-controller-fails-with-abstractcontrollerac

It was solved using Devise::TestHelpers.

require 'test_helper'

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

   test 'You can bring your self-introduction' do
       setup_controller_for_warden # This and
       request.env["device.mapping"] = Device.mapping[:user] # Add this part

      get :info, format: :js
      assert_response :success
    end
end


2022-09-21 15:27

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.