Skip to content

Commit 7cfd2ce

Browse files
committed
Merge pull request #29 from mapbox/1ec5-nocilla
Replace OHHTTPStubs with Nocilla
2 parents 0f92b3b + 7218075 commit 7cfd2ce

File tree

7 files changed

+127
-315
lines changed

7 files changed

+127
-315
lines changed

MapboxGeocoder.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
DA2E03F71CB0FFD800D1269A /* MBGeocoderConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA2E03F61CB0FFD800D1269A /* MBGeocoderConfiguration.swift */; };
1515
DA2E03F91CB0FFF700D1269A /* RequestKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DA2E03F81CB0FFF700D1269A /* RequestKit.framework */; };
1616
DA2E03FB1CB1009200D1269A /* MBGeocoderRouter.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA2E03FA1CB1009200D1269A /* MBGeocoderRouter.swift */; };
17+
DA701C011CB1292C00B0E520 /* MBGeocoderConfigurationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA701C001CB1292C00B0E520 /* MBGeocoderConfigurationTests.swift */; };
18+
DA701C031CB12A1800B0E520 /* Fixture.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA701C021CB12A1800B0E520 /* Fixture.swift */; };
1719
DD342B5619A140EE00219F77 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD342B5519A140EE00219F77 /* AppDelegate.swift */; };
1820
DD342B5819A140EE00219F77 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD342B5719A140EE00219F77 /* ViewController.swift */; };
1921
DDC2295C1A36074F006BE405 /* MapboxGeocoder.h in Headers */ = {isa = PBXBuildFile; fileRef = DDC229581A36073B006BE405 /* MapboxGeocoder.h */; settings = {ATTRIBUTES = (Public, ); }; };
@@ -100,6 +102,8 @@
100102
DA2E03F61CB0FFD800D1269A /* MBGeocoderConfiguration.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MBGeocoderConfiguration.swift; sourceTree = "<group>"; };
101103
DA2E03F81CB0FFF700D1269A /* RequestKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = RequestKit.framework; path = "Pods/../build/Debug-iphoneos/RequestKit.framework"; sourceTree = "<group>"; };
102104
DA2E03FA1CB1009200D1269A /* MBGeocoderRouter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MBGeocoderRouter.swift; sourceTree = "<group>"; };
105+
DA701C001CB1292C00B0E520 /* MBGeocoderConfigurationTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MBGeocoderConfigurationTests.swift; sourceTree = "<group>"; };
106+
DA701C021CB12A1800B0E520 /* Fixture.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Fixture.swift; sourceTree = "<group>"; };
103107
DD342B5019A140EE00219F77 /* Geocoder (Swift).app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "Geocoder (Swift).app"; sourceTree = BUILT_PRODUCTS_DIR; };
104108
DD342B5419A140EE00219F77 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
105109
DD342B5519A140EE00219F77 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
@@ -278,9 +282,11 @@
278282
DDF1E84B1BD6F7BA00C40C78 /* MapboxGeocoderTests */ = {
279283
isa = PBXGroup;
280284
children = (
285+
DA701C001CB1292C00B0E520 /* MBGeocoderConfigurationTests.swift */,
281286
DDF1E84C1BD6F7BA00C40C78 /* ReverseGeocodingTests.swift */,
282287
DDF1E84E1BD6F7BA00C40C78 /* Info.plist */,
283288
DDF1E8571BD700EB00C40C78 /* Fixtures */,
289+
DA701C021CB12A1800B0E520 /* Fixture.swift */,
284290
);
285291
path = MapboxGeocoderTests;
286292
sourceTree = "<group>";
@@ -599,6 +605,8 @@
599605
buildActionMask = 2147483647;
600606
files = (
601607
DDF1E84D1BD6F7BA00C40C78 /* ReverseGeocodingTests.swift in Sources */,
608+
DA701C011CB1292C00B0E520 /* MBGeocoderConfigurationTests.swift in Sources */,
609+
DA701C031CB12A1800B0E520 /* Fixture.swift in Sources */,
602610
);
603611
runOnlyForDeploymentPostprocessing = 0;
604612
};

MapboxGeocoderTests/Fixture.swift

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import XCTest
2+
import Foundation
3+
4+
internal class Fixture {
5+
internal class func stringFromFileNamed(name: String) -> String {
6+
guard let path = NSBundle(forClass: self).pathForResource(name, ofType: "json") else {
7+
XCTAssert(false, "Fixture \(name) not found.")
8+
return ""
9+
}
10+
do {
11+
return try String(contentsOfFile: path, encoding: NSUTF8StringEncoding)
12+
} catch {
13+
XCTAssert(false, "Unable to decode fixture at \(path): \(error).")
14+
return ""
15+
}
16+
}
17+
18+
internal class func JSONFromFileNamed(name: String) -> [String: AnyObject] {
19+
guard let path = NSBundle(forClass: self).pathForResource(name, ofType: "json") else {
20+
XCTAssert(false, "Fixture \(name) not found.")
21+
return [:]
22+
}
23+
guard let data = NSData(contentsOfFile: path) else {
24+
XCTAssert(false, "No data found at \(path).")
25+
return [:]
26+
}
27+
do {
28+
return try NSJSONSerialization.JSONObjectWithData(data, options: []) as! [String: AnyObject]
29+
} catch {
30+
XCTAssert(false, "Unable to decode JSON fixture at \(path): \(error).")
31+
return [:]
32+
}
33+
}
34+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import XCTest
2+
import Nocilla
3+
@testable import MapboxGeocoder
4+
5+
let BogusToken = "pk.feedCafeDadeDeadBeef-BadeBede.FadeCafeDadeDeed-BadeBede"
6+
7+
class MBGeocoderConfigurationTests: XCTestCase {
8+
override func setUp() {
9+
super.setUp()
10+
LSNocilla.sharedInstance().start()
11+
}
12+
13+
override func tearDown() {
14+
LSNocilla.sharedInstance().clearStubs()
15+
LSNocilla.sharedInstance().stop()
16+
super.tearDown()
17+
}
18+
19+
func testConfiguration() {
20+
let subject = MBGeocoderConfiguration(BogusToken)
21+
XCTAssertEqual(subject.accessToken, BogusToken)
22+
XCTAssertEqual(subject.apiEndpoint, "https://api.mapbox.com")
23+
}
24+
}

MapboxGeocoderTests/ReverseGeocodingTests.swift

Lines changed: 58 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,127 +1,84 @@
11
import XCTest
2-
import MapboxGeocoder
2+
import Nocilla
33
import CoreLocation
4-
import OHHTTPStubs
4+
@testable import MapboxGeocoder
55

66
class ReverseGeocodingTests: XCTestCase {
77

8-
let accessToken = "pk.eyJ1IjoianVzdGluIiwiYSI6IlpDbUJLSUEifQ.4mG8vhelFMju6HpIY-Hi5A"
9-
8+
override func setUp() {
9+
super.setUp()
10+
LSNocilla.sharedInstance().start()
11+
}
12+
1013
override func tearDown() {
11-
super.tearDown()
12-
OHHTTPStubs.removeAllStubs()
14+
LSNocilla.sharedInstance().clearStubs()
15+
LSNocilla.sharedInstance().stop()
16+
super.setUp()
1317
}
1418

1519
func testValidReverseGeocode() {
16-
let resultsExpectation = expectationWithDescription("reverse geocode should return results")
17-
let descriptionExpectation = expectationWithDescription("reverse geocode should populate description")
18-
let nameExpectation = expectationWithDescription("reverse geocode should populate name")
19-
let locationExpectation = expectationWithDescription("reverse geocode should populate location")
20-
let scopeExpectation = expectationWithDescription("reverse geocode should populate scope")
21-
let countryCodeExpectation = expectationWithDescription("reverse geocode should populate ISO country code")
22-
let countryExpectation = expectationWithDescription("reverse geocode should populate country")
23-
let postalCodeExpectation = expectationWithDescription("reverse geocode should populate postal code")
24-
let administrativeAreaExpectation = expectationWithDescription("reverse geocode should populate administrative area")
25-
let subAdministrativeAreaExpectation = expectationWithDescription("reverse geocode should populate sub-administrative area")
26-
let localityExpectation = expectationWithDescription("reverse geocode should populate locality")
27-
let thoroughfareExpectation = expectationWithDescription("reverse geocode should populate thoroughfare")
28-
let subThoroughfareExpectation = expectationWithDescription("reverse geocode should populate sub-thoroughfare")
29-
let regionExpectation = expectationWithDescription("reverse geocode should populate region")
30-
let addressStreetExpectation = expectationWithDescription("reverse geocode should populate street in address dictionary")
31-
let addressCityExpectation = expectationWithDescription("reverse geocode should populate city in address dictionary")
32-
let addressStateExpectation = expectationWithDescription("reverse geocode should populate state in address dictionary")
33-
let addressCountryExpectation = expectationWithDescription("reverse geocode should populate country in address dictionary")
34-
let addressISOCountryCodeExpectation = expectationWithDescription("reverse geocode should populate ISO country code in address dictionary")
20+
let expectation = expectationWithDescription("reverse geocode should return results")
3521

36-
stub(isHost("api.mapbox.com")) { _ in
37-
let path = NSBundle(forClass: self.dynamicType).pathForResource("reverse_valid", ofType: "json")
38-
return OHHTTPStubsResponse(fileAtPath: path!, statusCode: 200, headers: nil)
39-
}
22+
let json = Fixture.stringFromFileNamed("reverse_valid")
23+
stubRequest("GET", "https://api.mapbox.com/geocoding/v5/mapbox.places/-95.78558,37.13284.json?access_token=\(BogusToken)").andReturn(200).withHeaders(["Content-Type": "application/json"]).withBody(json)
4024

41-
let geocoder = MBGeocoder(accessToken: accessToken)
25+
let geocoder = MBGeocoder(accessToken: BogusToken)
26+
var addressPlacemark: MBPlacemark! = nil
27+
var placePlacemark: MBPlacemark! = nil
4228
geocoder.reverseGeocodeLocation(
4329
CLLocation(latitude: 37.13284000, longitude: -95.78558000)) { (placemarks, error) in
44-
if let result = placemarks?.first where placemarks?.count > 0 {
45-
resultsExpectation.fulfill()
46-
if result.description == "3099 3100 Rd, Independence, Kansas 67301, United States" {
47-
descriptionExpectation.fulfill()
48-
}
49-
if result.name == "3099 3100 Rd" {
50-
nameExpectation.fulfill()
51-
}
52-
if let location = result.location where location.coordinate.latitude == 37.12787 &&
53-
location.coordinate.longitude == -95.783074 {
54-
locationExpectation.fulfill()
55-
}
56-
if result.scope == .Address {
57-
scopeExpectation.fulfill()
58-
}
59-
if result.ISOcountryCode == "US" {
60-
countryCodeExpectation.fulfill()
61-
}
62-
if result.country == "United States" {
63-
countryExpectation.fulfill()
64-
}
65-
if result.postalCode == "67301" {
66-
postalCodeExpectation.fulfill()
67-
}
68-
if result.administrativeArea == "Kansas" {
69-
administrativeAreaExpectation.fulfill()
70-
}
71-
if result.subAdministrativeArea == "Independence" {
72-
subAdministrativeAreaExpectation.fulfill()
73-
}
74-
if result.locality == "Independence" {
75-
localityExpectation.fulfill()
76-
}
77-
if result.thoroughfare == "3100 Rd" {
78-
thoroughfareExpectation.fulfill()
79-
}
80-
if result.subThoroughfare == "3099" {
81-
subThoroughfareExpectation.fulfill()
82-
}
83-
let southWest = CLLocationCoordinate2D(latitude: 37.109405, longitude: -95.783365)
84-
let northEast = CLLocationCoordinate2D(latitude: 37.208643, longitude: -95.781811)
85-
if result.region == MBRectangularRegion(southWest: southWest, northEast: northEast) {
86-
regionExpectation.fulfill()
87-
}
88-
if let street = result.addressDictionary?[MBPostalAddressStreetKey] as? String where street == "3099 3100 Rd" {
89-
addressStreetExpectation.fulfill()
90-
}
91-
if let city = result.addressDictionary?[MBPostalAddressCityKey] as? String where city == "Independence" {
92-
addressCityExpectation.fulfill()
93-
}
94-
if let state = result.addressDictionary?[MBPostalAddressStateKey] as? String where state == "Kansas" {
95-
addressStateExpectation.fulfill()
96-
}
97-
if let country = result.addressDictionary?[MBPostalAddressCountryKey] as? String where country == "United States" {
98-
addressCountryExpectation.fulfill()
99-
}
100-
if let countryCode = result.addressDictionary?[MBPostalAddressISOCountryCodeKey] as? String where countryCode == "US" {
101-
addressISOCountryCodeExpectation.fulfill()
102-
}
103-
}
30+
XCTAssertEqual(placemarks?.count, 5, "reverse geocode should have 5 results")
31+
addressPlacemark = placemarks![0]
32+
placePlacemark = placemarks![1]
33+
34+
expectation.fulfill()
10435
}
10536

10637
waitForExpectationsWithTimeout(1) { (error) in
10738
XCTAssertNil(error, "Error: \(error)")
10839
XCTAssertFalse(geocoder.geocoding)
10940
}
41+
42+
XCTAssertEqual(addressPlacemark.description, "3099 3100 Rd, Independence, Kansas 67301, United States", "reverse geocode should populate description")
43+
XCTAssertEqual(addressPlacemark.name, "3099 3100 Rd", "reverse geocode should populate name")
44+
XCTAssertEqual(addressPlacemark.location?.coordinate, CLLocationCoordinate2D(latitude: 37.12787, longitude: -95.783074), "reverse geocode should populate location")
45+
XCTAssertEqual(addressPlacemark.scope, .Address, "reverse geocode should populate scope")
46+
XCTAssertEqual(addressPlacemark.ISOcountryCode, "US", "reverse geocode should populate ISO country code")
47+
XCTAssertEqual(addressPlacemark.country, "United States", "reverse geocode should populate country")
48+
XCTAssertEqual(addressPlacemark.postalCode, "67301", "reverse geocode should populate postal code")
49+
XCTAssertEqual(addressPlacemark.administrativeArea, "Kansas", "reverse geocode should populate administrative area")
50+
XCTAssertEqual(addressPlacemark.subAdministrativeArea, "Independence", "reverse geocode should populate sub-administrative area")
51+
XCTAssertEqual(addressPlacemark.locality, "Independence", "reverse geocode should populate locality")
52+
XCTAssertEqual(addressPlacemark.thoroughfare, "3100 Rd", "reverse geocode should populate thoroughfare")
53+
XCTAssertEqual(addressPlacemark.subThoroughfare, "3099", "reverse geocode should populate sub-thoroughfare")
54+
55+
XCTAssertNotNil(addressPlacemark.addressDictionary)
56+
let addressDictionary = addressPlacemark.addressDictionary!
57+
XCTAssertEqual(addressDictionary[MBPostalAddressStreetKey] as? String, "3099 3100 Rd", "reverse geocode should populate street in address dictionary")
58+
XCTAssertEqual(addressDictionary[MBPostalAddressCityKey] as? String, "Independence", "reverse geocode should populate city in address dictionary")
59+
XCTAssertEqual(addressDictionary[MBPostalAddressStateKey] as? String, "Kansas", "reverse geocode should populate state in address dictionary")
60+
XCTAssertEqual(addressDictionary[MBPostalAddressCountryKey] as? String, "United States", "reverse geocode should populate country in address dictionary")
61+
XCTAssertEqual(addressDictionary[MBPostalAddressISOCountryCodeKey] as? String, "US", "reverse geocode should populate ISO country code in address dictionary")
62+
63+
let southWest = CLLocationCoordinate2D(latitude: 37.033229992893, longitude: -95.927990005645)
64+
let northEast = CLLocationCoordinate2D(latitude: 37.35632800706, longitude: -95.594628992671)
65+
let region = placePlacemark.region
66+
XCTAssertNotNil(region, "reverse geocode should populate region")
67+
XCTAssertEqualWithAccuracy(region!.southWest.latitude, southWest.latitude, accuracy: 0.000000000001)
68+
XCTAssertEqualWithAccuracy(region!.southWest.longitude, southWest.longitude, accuracy: 0.000000000001)
69+
XCTAssertEqualWithAccuracy(region!.northEast.latitude, northEast.latitude, accuracy: 0.000000000001)
70+
XCTAssertEqualWithAccuracy(region!.northEast.longitude, northEast.longitude, accuracy: 0.000000000001)
11071
}
11172

11273
func testInvalidReverseGeocode() {
113-
let resultsExpection = expectationWithDescription("reverse geocode should return no results for invalid query")
114-
115-
stub(isHost("api.mapbox.com")) { _ in
116-
let path = NSBundle(forClass: self.dynamicType).pathForResource("reverse_invalid", ofType: "json")
117-
return OHHTTPStubsResponse(fileAtPath: path!, statusCode: 200, headers: nil)
118-
}
119-
120-
let geocoder = MBGeocoder(accessToken: accessToken)
74+
let json = Fixture.stringFromFileNamed("reverse_invalid")
75+
stubRequest("GET", "https://api.mapbox.com/geocoding/v5/mapbox.places/0.00000,0.00000.json?access_token=\(BogusToken)").andReturn(200).withHeaders(["Content-Type": "application/json"]).withBody(json)
76+
77+
let expection = expectationWithDescription("reverse geocode execute completion handler for invalid query")
78+
let geocoder = MBGeocoder(accessToken: BogusToken)
12179
geocoder.reverseGeocodeLocation(CLLocation(latitude: 0, longitude: 0)) { (placemarks, error) in
122-
if placemarks?.count == 0 {
123-
resultsExpection.fulfill()
124-
}
80+
XCTAssertEqual(placemarks?.count, 0, "reverse geocode should return no results for invalid query")
81+
expection.fulfill()
12582
}
12683

12784
waitForExpectationsWithTimeout(1) { (error) in
Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1 @@
1-
{
2-
"type": "FeatureCollection",
3-
"query": [
4-
0,
5-
0
6-
],
7-
"features": [
8-
9-
],
10-
"attribution": "NOTICE: \u00a9 2016 Mapbox and its suppliers. All rights reserved. Use of this data is subject to the Mapbox Terms of Service (https:\/\/www.mapbox.com\/about\/maps\/). This response and the information it contains may not be retained."
11-
}
12-
1+
{"type":"FeatureCollection","query":[0,0],"features":[],"attribution":"NOTICE: © 2016 Mapbox and its suppliers. All rights reserved. Use of this data is subject to the Mapbox Terms of Service (https://www.mapbox.com/about/maps/). This response and the information it contains may not be retained."}

0 commit comments

Comments
 (0)