Ich verwende den Geocoder Edelstein, aber ich weiß nicht, in welche Datei muss ich einfügen this code. Kannst du mir erzählen ?Testen mit Geocoder Edelstein
Antwort
Dieser Code sollte sich im SETUP-Teil des von Ihnen verwendeten Test-Frameworks befinden.
Wenn rspec verwenden, soll es hier gehen:
describe Something do
before(:all) do
Geocoder.configure(:lookup => :test)
Geocoder::Lookup::Test.add_stub(
"New York, NY", [
{
'latitude' => 40.7143528,
'longitude' => -74.0059731,
'address' => 'New York, NY, USA',
'state' => 'New York',
'state_code' => 'NY',
'country' => 'United States',
'country_code' => 'US'
}
]
)
end
end
Eine Alternative Ihre Stümpfe in dem Testaufbau zu setzen ist, sie in spec/support
zu definieren:
spec/support/geocoder.rb
Während dieser Ansatz den Nachteil hat, mysteriöse Gäste in Ihre Tests einzubeziehen, führt dies zu DRY-Problemen.
Vergessen Sie nicht, diese in 'module' mit einem Namen Ihrer Wahl zu wickeln, wenn Sie Fügen Sie es in 'spec/rails_helper.rb' mit' config.include yourModuleName' ein –
Da Sie Ihren Testframework nicht angegeben haben, gebe ich eine spezifische Antwort.
Ich benutze Gurke und Rspec. Während all dies von @DevDude und @malandrina zutrifft, hier ein vollständigerer Tipp, wo der Code hingehen kann und wie man auch Einträge für umgekehrte Geokodierung (lat/lon -> Adresse) hinzufügt:
Setzen Sie Ihre Stubs in den Spezifikationsordner. Ich habe ein Array von Arrays, so dass ich mehr "Lookups" hinzufügen könnte zu stubbed out:
spec/support/geocoder_stubs.rb
addresses = {
"230 West 43rd St., New York City, NY 10036" => {
'latitude' => 40.7573862,
'longitude' => -73.9881256,
'address' => '230 West 43rd St., New York City, NY 10036',
'city' => 'New York City',
'state' => 'New York',
'state_code' => 'NY',
'country' => 'United States',
'country_code' => 'US'
},
[40.75747130000001, -73.9877319] => {
'latitude' => 40.75747130000001,
'longitude' => -73.9877319,
'address' => '229 West 43rd St., New York City, NY 10036',
'city' => 'New York City',
'state' => 'New York',
'state_code' => 'NY',
'country' => 'United States',
'country_code' => 'US'
},
"Worthington, OH" => {
'latitude' => 40.09846115112305,
'longitude' => -83.01747131347656,
'address' => 'Worthington, OH',
'city' => 'Worthington',
'state' => 'Ohio',
'state_code' => 'OH',
'country' => 'United States',
'country_code' => 'US'
},
}
Geocoder.configure(:lookup => :test)
addresses.each { |lookup, results| Geocoder::Lookup::Test.add_stub(lookup, [results]) }
Referenz Ihrer Stubs im Gurke Unterstützung Ordnern:
features/support/env.rb
require Rails.root.join("spec/support/geocoder_stubs")
hoffte, das hilft!
Ich setze diesen Code in meine /config/initializers/geocoder.rb
mit einer Bedingung für Rails.env.test?
. Ich habe versucht, oben genannten Ansätze von @devDude, es funktionierte großartig, aber ich wollte nur keine Geocoding echte Anrufe von meinen RSPECT Tests gemacht werden, auch aus Versehen (hatte eine Menge von Spezifikationen in vielen Dateien davon abhängig) + Dieser Ansatz funktioniert für jede Art von Test-Framework (sei es Testeinheit oder Mintests oder auch mit Gurke).
So sieht meine /config/initializers/geocoder.rb
Datei aus.
if Rails.env.test?
Geocoder.configure(:lookup => :test)
# Particular Look up
Geocoder::Lookup::Test.add_stub(
"New York, NY", [
{
'latitude' => 40.7143528,
'longitude' => -74.0059731,
'address' => 'New York, NY, USA',
'state' => 'New York',
'state_code' => 'NY',
'country' => 'United States',
'country_code' => 'US'
}
]
)
#default stub
Geocoder::Lookup::Test.set_default_stub(
[
{
'latitude' => 40.7143528,
'longitude' => -74.0059731,
'address' => 'New York, NY, USA',
'state' => 'New York',
'state_code' => 'NY',
'country' => 'United States',
'country_code' => 'US'
}
]
)
else
Geocoder.configure(
:timeout => 3, # geocoding service timeout (secs)
:lookup => :google, # name of geocoding service (symbol)
:language => :en, # ISO-639 language code
:units => :mi, # :km for kilometers or :mi for miles
:distances => :linear # :spherical or :linear
)
end
Ich verwende rspec ... –
Ich aktualisiert nur die Antwort, wie pro Ihr Feedback, haben ein gutes – DevDude