NoMethodError: undefined method `env' for nil:NilClass

I am using rails testing for my app and when I run this test

class ShowingQuizzesTest < ActionDispatch::IntegrationTest
    include Devise::TestHelpers
    setup { host! 'example.com' }
    test 'return quiz by id' do 
        instructor=Instructor.create(name:"same7", email:"[email protected]", password:"123abc")
        sign_in instructor
        quiz = Quiz.create(name: 'Quiz1', subject: 'physics', duration: 10, no_of_MCQ: 5, no_of_rearrangeQ: 5) 
        current_instructor.quizzes << quiz
        get "api/quizzes/#{quiz.id}"
        assert_equal 200, response.status

        quiz_response = json(response.body)
        assert_equal quiz.name, quiz_response[:name]
    end
end     

This error appears: 1) Error: ShowingQuizzesTest#test_return_quiz_by_id: NoMethodError: undefined method `env' for nil:NilClass

the method I am testing:

   def show
            current_instructor.quizzes.find(params[:id])
            render json: data:{:quiz => quiz}, status: 200
        end

Upvotes: 1

Views: 419

Answers (1)

Mirv - Matt
Mirv - Matt

Reputation: 554

Are you on Rails 5?

It looks like Rails 5 deprecated the old method of including test helpers - as the Rails team has made significant improvements to the speed of integration tests. Currently, all controllers are tested in integration tests - Link

I'm getting the same error when dealing with devise & testing. I was able to make the error go away by commenting out # include Devise::Test::ControllerHelpers from my test file or the test_helpers.rb if you inherit your test helpers there.

Upvotes: 1

Related Questions