-
Notifications
You must be signed in to change notification settings - Fork 0
/
code_spec.rb
85 lines (71 loc) · 2.65 KB
/
code_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
require_relative 'code'
require 'rspec'
require 'webmock/rspec'
require 'open-uri'
RSpec.describe "When importing lat and lng" do
url = 'https://www.marinetraffic.com/en/ais/details/ships/shipid:4199684/mmsi:244670249/vessel:STORMALONG'
it 'checks your actual connection is working' do
WebMock.allow_net_connect!
response = open('http://google.com')
expect(response.status).to eq(["200", "OK"])
end
it 'returns error if timeout' do
check_timeout = WebMock.stub_request(:get, url).to_timeout
expect{ find_string(check_timeout) }.to raise_error(StandardError)
end
it 'returns error if 500' do
check_500 = WebMock.stub_request(:get, url).to_return(status: 500)
expect{ find_string(check_500) }.to raise_error(StandardError)
end
it 'returns error if 404' do
check_404 = WebMock.stub_request(:get, url).to_return(status: 404)
expect{ find_string(check_404) }.to raise_error(StandardError)
end
it "returns NoMethodError error if no HTML is found" do
expect{ find_string('Not HTML') }.to raise_error(NoMethodError)
end
# Could find_array be considered a private method? If so,
# we shouldn't be testing it (according to Sandi Metz)
it "returns only one value if we split using wrong divider" do
array = find_array('1.1° / 9.9°', '&')
expect(array.length).to eq(1)
end
# this is connected to the previous one
it "returns error if you get only one value" do
find_position = find_lat_lng(['33'])
puts find_position
expect(find_position).to eq(@error)
end
it "returns error if lat / lng are not numbers" do
find_position = find_lat_lng(['foo', 'bar'])
puts find_position
expect(find_position).to eq(@error)
end
# We assume the parsing logic is correct if we get values
# close to each other after a short time
it "shouldn't be too far from the previous position" do
WebMock.allow_net_connect!
prev_lat, prev_lng = find_lat_lng
sleep 10 # change this to whatever value makes sense
current_lat, current_lng = find_lat_lng
expect(current_lat).to be_within(0.2).of(prev_lat)
expect(current_lng).to be_within(0.2).of(prev_lng)
end
it "returns error if lat is out of limits" do
lat_lng = find_lat_lng(['99.9', '22.2'])
puts lat_lng
expect(lat_lng).to eq(@error)
end
it "returns error if lng is out of limits" do
lat_lng = find_lat_lng(['11.1', '-199.9'])
puts lat_lng
expect(lat_lng).to eq(@error)
end
it "checks lat and lng exist in the world" do
WebMock.allow_net_connect!
# the example we wrote together
lat, lng = find_lat_lng
expect(lat).to be_between(-90, 90)
expect(lng).to be_between(-180, 180)
end
end