내 Rails 앱에서 사용자가 연결하는 국가의 지오 코더 덕분에 감지하는 기능을 사용하고 있습니다. 그때 나는 그 내용을 기반으로 콘텐츠를 제공합니다.사용자가 특정 국가 (레일 4, 지오 코더, rspec)에서 연결할 때 올바른 i18n 콘텐츠가 사용되는지 확인하는 Rspec 기능 테스트
내가
- 독일 사용 프랑스에서 연결하는 경우, 그는합니다 (fr.yml 파일에서) "봉쥬르 쉐어의 utilisateur"를 보는 것을 예를 들어 확인하기 위해 RSpec에 기능 테스트를 만들고 싶습니다 독일 사용 프랑스에서 연결하는 경우
- , 그는 내가 "모의"이 방법 "그루터기"웹에서 확인하거나 시도한
합니다 (en.yml 파일에서) "안녕 사랑하는 사용자"를보고 , 그러나 나는 그것을하는 방법을 모른다 (아직도 초보자). 그게 내가 시도 무엇하지만 실패 여기
describe "the right locale is generated when user visits homepage from the US" do
request.location.country= 'US'
visit root_path
expect(page).to have_content 'Hi dear user'
end
파일이 올바른 언어를
컨트롤러/문제/
def set_location_by_ip_lookup
if Rails.env.development?
Geocoder.search(request.remote_ip).first
else
request.location
end
end
컨트롤러/문제/CountrySetter 위치 정보를 확인하고 봉사하는 날을 가능하게하는 로케일 설정자
def set_locale
I18n.locale = extract_locale_from_country
end
def extract_locale_from_country
case I18nData.country_code(set_location_by_ip_lookup.country)
# codes here https://www.iso.org/obp/ui/#search
when 'US'
'en'
when 'FR'
'fr'
when 'ES'
'es'
when 'DE'
'de'
when 'NL'
'nl'
else
'en'
end
end
편집
내가
describe "A connection from an IP located in Germany triggers country=Germany (used in LocaleSetter and CountrySetter) " do
let(:subject) { ApplicationController.new }
it "loads the right locale on the HP" do
allow_any_instance_of(ActionDispatch::Request).to receive(:remote_ip) {'77.185.208.234'}
subject.class.skip_before_filter :set_location_by_ip_lookup
subject.class.skip_before_filter :set_locale
expect(request.location.country).to eq("Germany")
end
end
을 시도 내가 오류가 점점 오전 : IP로 처리 될 때
ApplicationController A connection from an IP located in Germany triggers country=Germany (used in LocaleSetter and CountrySetter) loads the right locale on the HP
Failure/Error: expect(request.location.country).to eq("Germany")
expected: "Germany"
got: "Reserved"
(compared using ==)
Finished in 0.22149 seconds (files took 24 minutes 5 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./spec/controllers/application_controller_spec.rb:10
그것은 내가 실제로 가지고 다른 질문을 기반으로 보인다, 즉 지오 코더 반환 "예약" 로컬 (127.0.0 또는 00.0.00.0) : Geocoder Gem - getting "Reserved" for request.location.country (even in Local!)
그래서 rspec이 이해하지 못하는 것 같습니다. remote_ip = 77.185.208.234
감사합니다. 나는 당신이 rspec/기능에서 i18n을 테스트하고 다른 테스트를 컨트롤러로 옮겨야한다는 것이 옳다고 생각합니다. 그러나, 당신이 조언을 구현하고 오류가 발생했습니다 => 내 게시물에 편집을 참조하십시오 – Mathieu
나는 내 이전의 대답을 재고하고 그것을 편집했습니다. 외부 공급자를 사용하고 테스트를 작성할 때 외부 API에 의존해서는 안되기 때문에 지오 코더에 IP 조회를 요청하는 것은 좋지 않습니다. 또한 지오 코더 작성자가 이미 테스트 한 것을 테스트 할 필요가 없습니다. 편집 된 코드를 검사하십시오. 논리의 테스트가 없습니다. ip lookups가 올바르게 작동하고 그렇지 않은 경우 지오 코더 github 프로젝트에 문제를 제기하십시오. 테스트하려는 것은 인덱스 페이지를 방문 할 때 IP 조회가 시작되고 적절한 지오 코더 결과에 따라 올바른 로케일을 설정하는 것입니다. –