이것은 문제가 아니며, 내가 발견 한 해결책이다.'NoMethodError : 정의되지 않은 메소드`nil : NilClass'에 대해 레일을 이용한 기능 테스트를 할 때
저는 스페인어, 영어 및 일본어로 텍스트를 표시하는 Ruby on Rails 4.1을 사용하여 응용 프로그램을 개발 중입니다.
나는 기능 테스트를 시작했을 때, 나는 다음과 같은 오류가 계속 : 전무에 대한 정의되지 않은 메서드 '스캔':
NoMethodError을 내가 같은 오류가 여러 게시물을 본
Surffing을 NilClass을하지만, 나에게는 아무런 효과가 없다.
application_controller.rb :
require 'test_helper'
class HomesControllerTest < ActionController::TestCase
test "should get index" do
get :index
assert_response :success
end
end
:
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_action :set_locale
def set_locale
I18n.locale = params[:locale] || I18n.default_locale
navegador = extract_locale_from_accept_language_header
ruta = params[:locale] || nil
unless ruta.blank?
I18n.locale = ruta if IDIOMAS.flatten.include? ruta
else
I18n.locale = navegador if IDIOMAS.flatten.include? navegador
end
end
private
def extract_locale_from_accept_language_header
request.env['HTTP_ACCEPT_LANGUAGE'].scan(/^[a-z]{2}/).first
end
def ajusta_pagina_filtro
if defined? params[:post][:filtrar_por]
buscar = params[:post][:filtrar_por]
else
buscar = ''
end
page = params[:pagina] || 1
[page, buscar]
end
end
그래서이에서 /test/controllers/homes_controller_test.rb에 대한 코드가
이
코드 원래 코드그래서 '갈퀴 테스트'를 할 때 나는 다음과 같은 결과를 얻었습니다.
application_controller.rb, 상기 방법에 extract_locale_from_accept_language_header가 failling된다1) Error:
HomesControllerTest#test_should_get_index:
NoMethodError: undefined method `scan' for nil:NilClass
app/controllers/application_controller.rb:22:in `extract_locale_from_accept_language_header'
app/controllers/application_controller.rb:9:in `set_locale'
test/controllers/homes_controller_test.rb:5:in `block in <class:HomesControllerTest>'
가능한 중복 [무엇 널 (null) 포인터 예외이며, 어떻게 그것을 해결합니까?] (HTTP begin을 구조 블록없이 작동합니다 : //stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) –