Skip to content

Commit

Permalink
using rspec's let feature to great effect.
Browse files Browse the repository at this point in the history
Two changes here: the first is to use an awesome feature of rspec: let.

Basically, we can let rspec instantiate our object, which gets
all of that typing of long class names out of the way. It also lets
our tests focus on what's important, and ignore what isn't. Read the tests
now: "When the ip is this, I expect the locator to do that."

The second thing was breaking the last test into two. I wanted to do
this as a separate step, but it didn't work until I made the change:
you really had two assertions in that last one. It deserves its own
test.
  • Loading branch information
steveklabnik committed Jan 12, 2012
1 parent 8feb700 commit 664ec18
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions spec/geolocator_spec.rb
Expand Up @@ -2,6 +2,7 @@
require 'geolocator'

describe Geolocator do
let(:locator) { Geolocator.new(@ip) }

describe "#ip_lookup" do
it "throws an ArgumentError error if no parameters passed" do
Expand All @@ -13,32 +14,43 @@
end

it "only accepts input that passes a basic IP regex" do
expect {Geolocator.new("sdfsadfsdf").ip_lookup}.to raise_error(RuntimeError,"Not a valid IPv4 address")
@ip = "sdfsadfsdf"
expect {locator.ip_lookup}.to raise_error(RuntimeError,"Not a valid IPv4 address")
end

it "should not accept localhost/loopback address" do
expect {Geolocator.new("127.0.0.1").ip_lookup}.to raise_error(RuntimeError,"Can't lookup localhost address. Please use an external IP address!")
@ip = "127.0.0.1"
expect {locator.ip_lookup}.to raise_error(RuntimeError,"Can't lookup localhost address. Please use an external IP address!")
end
end
end

describe Geolocator::FreeGeoIp do
let(:geo_ip) { Geolocator::FreeGeoIp.new(@ip) }
describe "#geolocate_ip" do
it "successfully makes a HTTP request" do
expect{Geolocator::FreeGeoIp.new("231.4.8.6").geolocate_ip.success?}.to be_true
@ip = "231.4.8.6"
expect{geo_ip.geolocate_ip.success?}.to be_true
end

it "throws an error for HTTP statuses other than 200" do
expect {Geolocator::FreeGeoIp.new("0.0.0.0").geolocate_ip}.to raise_error(RuntimeError,"IP address not found")
@ip = "0.0.0.0"
expect {geo_ip.geolocate_ip}.to raise_error(RuntimeError,"IP address not found")
end

it "parses the HTTP body with JSON" do
Geolocator::FreeGeoIp.new("123.45.6.28").geolocate_ip.should be_an_instance_of Hash
@ip = "123.45.6.28"
geo_ip.geolocate_ip.should be_an_instance_of Hash
end

it "has a value for city" do
@result = Geolocator::FreeGeoIp.new("123.45.6.28").geolocate_ip["city"].should_not be_empty
expect {Geolocator::FreeGeoIp.new("240.0.0.0").geolocate_ip}.to raise_error(RuntimeError,"Incomplete record. Please try another address")
@ip = "123.45.6.28"
geo_ip.geolocate_ip["city"].should_not be_empty
end

it "raises a RuntimeError if there isn't a city" do
@ip = "240.0.0.0"
expect {geo_ip.geolocate_ip}.to raise_error(RuntimeError,"Incomplete record. Please try another address")
end
end
end
Expand Down

0 comments on commit 664ec18

Please sign in to comment.