|
9 | 9 |
|
10 | 10 | import Quick |
11 | 11 | import Nimble |
| 12 | +import Swinject |
12 | 13 | @testable import SwinjectSimpleExample |
13 | 14 |
|
14 | 15 | 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 | + |
15 | 47 | 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 | + |
16 | 58 | it("returns cities.") { |
17 | 59 | var cities: [City]? |
18 | | - let fetcher = WeatherFetcher(networking: Network()) |
| 60 | + let fetcher = container.resolve(WeatherFetcher.self)! |
19 | 61 | fetcher.fetch { cities = $0 } |
20 | 62 |
|
21 | 63 | 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")) |
26 | 77 | } |
27 | 78 | } |
28 | 79 | } |
0 commit comments