我正在为我的应用程序和运行此测试时使用rails测试
class ShowingQuizzesTest < ActionDispatch::IntegrationTest
include Devise::TestHelpers
setup { host! 'example.com' }
test 'return quiz by id' do
instructor=Instructor.create(name:"same7", email:"abcdefg@eng.asu.edu.com", 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
出现此错误: 1)错误: ShowingQuizzesTest#test_return_quiz_by_id: NoMethodError:nil的未定义方法`env':NilClass
我正在测试的方法:
def show
current_instructor.quizzes.find(params[:id])
render json: data:{:quiz => quiz}, status: 200
end
答案 0 :(得分:0)
你是否在Rails 5上?
看起来Rails 5不赞成包含测试助手的旧方法 - 因为Rails团队已经对集成测试的速度做出了重大改进。目前,所有控制器都在集成测试中进行测试 - Link
我在处理设计时遇到了同样的错误。测试。通过在我的测试文件中注释掉# include Devise::Test::ControllerHelpers
或者在那里继承了测试助手test_helpers.rb
,我能够消除错误。