Skip to content
This repository was archived by the owner on Aug 21, 2021. It is now read-only.

Commit 4d884fc

Browse files
committed
Add a spec to fill weather data from JSON.
1 parent 3a31fd5 commit 4d884fc

File tree

1 file changed

+56
-5
lines changed

1 file changed

+56
-5
lines changed

SwinjectSimpleExampleTests/WeatherFetcherSpec.swift

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,71 @@
99

1010
import Quick
1111
import Nimble
12+
import Swinject
1213
@testable import SwinjectSimpleExample
1314

1415
class WeatherFetcherSpec: QuickSpec {
16+
struct StubNetwork: Networking {
17+
private static let json =
18+
"{" +
19+
"\"list\": [" +
20+
"{" +
21+
"\"id\": 2643743," +
22+
"\"name\": \"London\"," +
23+
"\"weather\": [" +
24+
"{" +
25+
"\"main\": \"Rain\"" +
26+
"}" +
27+
"]" +
28+
"}," +
29+
"{" +
30+
"\"id\": 3451190," +
31+
"\"name\": \"Rio de Janeiro\"," +
32+
"\"weather\": [" +
33+
"{" +
34+
"\"main\": \"Clear\"" +
35+
"}" +
36+
"]" +
37+
"}" +
38+
"]" +
39+
"}"
40+
41+
func request(response: NSData? -> ()) {
42+
let data = StubNetwork.json.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
43+
response(data)
44+
}
45+
}
46+
1547
override func spec() {
48+
let container = Container()
49+
container.register(Networking.self) { _ in Network() }
50+
container.register(WeatherFetcher.self) { r in
51+
WeatherFetcher(networking: r.resolve(Networking.self)!)
52+
}
53+
container.register(Networking.self, name: "stub") { _ in StubNetwork() }
54+
container.register(WeatherFetcher.self, name: "stub") { r in
55+
WeatherFetcher(networking: r.resolve(Networking.self, name: "stub")!)
56+
}
57+
1658
it("returns cities.") {
1759
var cities: [City]?
18-
let fetcher = WeatherFetcher(networking: Network())
60+
let fetcher = container.resolve(WeatherFetcher.self)!
1961
fetcher.fetch { cities = $0 }
2062

2163
expect(cities).toEventuallyNot(beNil())
22-
expect(cities?.count).toEventually(equal(12))
23-
expect(cities?[0].id).toEventually(equal(6077243))
24-
expect(cities?[0].name).toEventually(equal("Montreal"))
25-
expect(cities?[0].weather).toEventually(equal("Clouds"))
64+
expect(cities?.count).toEventually(beGreaterThan(0))
65+
}
66+
it("fills weather data.") {
67+
var cities: [City]?
68+
let fetcher = container.resolve(WeatherFetcher.self, name: "stub")!
69+
fetcher.fetch { cities = $0 }
70+
71+
expect(cities?[0].id).toEventually(equal(2643743))
72+
expect(cities?[0].name).toEventually(equal("London"))
73+
expect(cities?[0].weather).toEventually(equal("Rain"))
74+
expect(cities?[1].id).toEventually(equal(3451190))
75+
expect(cities?[1].name).toEventually(equal("Rio de Janeiro"))
76+
expect(cities?[1].weather).toEventually(equal("Clear"))
2677
}
2778
}
2879
}

0 commit comments

Comments
 (0)