ruby - Rspec feature test to check that the right i18n content is used when user connects from certain countries (rails 4, geocoder, rspec) -
i using in rails app feature detects geocoder country th user connects serve content based on that.
i'd create rspec features tests sure illustration
if high german used connects france, sees "bonjour cher utilisateur" (from fr.yml file) if high german used connects france, sees "hi dear user" (from en.yml file)i've tried check on web how "stub" or "mock" this, don't know how (still newbie). that's have tried fails:
describe "the right locale generated when user visits homepage us" request.location.country= 'us' visit root_path expect(page).to have_content 'hi dear user' end here files enabling me geolocate , serve right language
controllers/concerns/countrysetter
def set_location_by_ip_lookup if rails.env.development? geocoder.search(request.remote_ip).first else request.location end end controller/concerns/localesetter
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 edit
i tried
describe "a connection ip located in federal republic of germany triggers country=germany (used in localesetter , countrysetter) " let(:subject) { applicationcontroller.new } "loads right locale on hp" 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 and getting error:
applicationcontroller connection ip located in federal republic of germany triggers country=germany (used in localesetter , countrysetter) loads right locale on 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 load) 1 example, 1 failure failed examples: rspec ./spec/controllers/application_controller_spec.rb:10 it seems based on question had actually, geocoder returns "reserved" when ip treated local (127.0.0 or 00.0.00.0) : see geocoder gem - getting "reserved" request.location.country (even in local!)
so seems rspec not understand trying give him remote_ip = 77.185.208.234
visit capybara method, starting browser driver , hence has session in separate thread. because of it, cannot see rack request instance, creates separate one.
in feature spec have stub any_instance of rack request. can stub location method (coming geocoder) homecoming geocoder::result this:
#rspec 3 syntax allow_any_instance_of(actiondispatch::request).to receive(:location) instance_double("geocoder::result::freegeoip", :country => 'united states') end and don't have test whole country logic in feature spec. cases can rely on rails i18n work correctly 1 time set , configured. test :set_locale logic in controller/request spec separtely , check i18n.locale. in request spec, there no capybara session , have direct access controller instance or it's request
#spec/controllers/application_controller_spec.rb describe applicationcontroller describe "get index" "sets location ip lookup" allow(request).to recieve(:location)do instance_double("geocoder::result::freegeoip", :country => 'united states') end "index" expect(i18n.locale).to eq 'en' end end end note:
geocoder has country_code method providers.
instead of
i18ndata.country_code(set_location_by_ip_lookup.country) use
set_location_by_ip_lookup.country_code and stub accordingly in tests
ruby-on-rails ruby ruby-on-rails-3 rspec rails-geocoder
No comments:
Post a Comment