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

Commit 6851935

Browse files
committed
Merge branch 'make-it-injectable'
2 parents 6d64df1 + 4d884fc commit 6851935

File tree

7 files changed

+109
-12
lines changed

7 files changed

+109
-12
lines changed

Podfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use_frameworks!
44

55
pod 'Alamofire', :git => 'https://github.com/Alamofire/Alamofire.git', :commit => '1b7b1f1aa'
66
pod 'SwiftyJSON', :git => 'https://github.com/SwiftyJSON/SwiftyJSON.git', :commit => '45ca854ce'
7+
pod 'Swinject', '~> 0.2'
78

89

910
def testing_pods

Podfile.lock

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ PODS:
33
- Nimble (2.0.0-rc.2)
44
- Quick (0.5.1)
55
- SwiftyJSON (2.2.1)
6+
- Swinject (0.2)
67

78
DEPENDENCIES:
89
- Alamofire (from `https://github.com/Alamofire/Alamofire.git`, commit `1b7b1f1aa`)
910
- Nimble (= 2.0.0-rc.2)
1011
- Quick (~> 0.5.0)
1112
- SwiftyJSON (from `https://github.com/SwiftyJSON/SwiftyJSON.git`, commit `45ca854ce`)
13+
- Swinject (~> 0.2)
1214

1315
EXTERNAL SOURCES:
1416
Alamofire:
@@ -31,5 +33,6 @@ SPEC CHECKSUMS:
3133
Nimble: e3cf5e73a491daf21c2bc88783dbfc55ce8b8e02
3234
Quick: 7426537a99e75076d6930c5b6da58006e4a9f38a
3335
SwiftyJSON: ae2d0a3d68025d136602a33c4ee215091ced3e33
36+
Swinject: 08c22f85e8f073257befa6847485e348c8f13825
3437

3538
COCOAPODS: 0.38.2

SwinjectSimpleExample.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
9878A1E81B78745C0005ACD9 /* WeatherFetcher.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9878A1E61B78745C0005ACD9 /* WeatherFetcher.swift */; };
1313
9878A1EB1B78746C0005ACD9 /* WeatherFetcherSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9878A1EA1B78746C0005ACD9 /* WeatherFetcherSpec.swift */; };
1414
9878A1ED1B7876A70005ACD9 /* OpenWeatherMap.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9878A1EC1B7876A70005ACD9 /* OpenWeatherMap.swift */; };
15+
9878A1EF1B78930E0005ACD9 /* Networking.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9878A1EE1B78930E0005ACD9 /* Networking.swift */; };
16+
9878A1F11B7893210005ACD9 /* Network.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9878A1F01B7893210005ACD9 /* Network.swift */; };
1517
98BA27341B785EB00047549E /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 98BA27331B785EB00047549E /* AppDelegate.swift */; };
1618
98BA27361B785EB00047549E /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 98BA27351B785EB00047549E /* ViewController.swift */; };
1719
98BA27391B785EB00047549E /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 98BA27371B785EB00047549E /* Main.storyboard */; };
@@ -36,6 +38,8 @@
3638
9878A1E61B78745C0005ACD9 /* WeatherFetcher.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WeatherFetcher.swift; sourceTree = "<group>"; };
3739
9878A1EA1B78746C0005ACD9 /* WeatherFetcherSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WeatherFetcherSpec.swift; sourceTree = "<group>"; };
3840
9878A1EC1B7876A70005ACD9 /* OpenWeatherMap.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OpenWeatherMap.swift; sourceTree = "<group>"; };
41+
9878A1EE1B78930E0005ACD9 /* Networking.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Networking.swift; sourceTree = "<group>"; };
42+
9878A1F01B7893210005ACD9 /* Network.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Network.swift; sourceTree = "<group>"; };
3943
98BA27301B785EB00047549E /* SwinjectSimpleExample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SwinjectSimpleExample.app; sourceTree = BUILT_PRODUCTS_DIR; };
4044
98BA27331B785EB00047549E /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
4145
98BA27351B785EB00047549E /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };
@@ -89,6 +93,8 @@
8993
9878A1E51B78745C0005ACD9 /* City.swift */,
9094
9878A1E61B78745C0005ACD9 /* WeatherFetcher.swift */,
9195
9878A1EC1B7876A70005ACD9 /* OpenWeatherMap.swift */,
96+
9878A1EE1B78930E0005ACD9 /* Networking.swift */,
97+
9878A1F01B7893210005ACD9 /* Network.swift */,
9298
);
9399
name = Model;
94100
sourceTree = "<group>";
@@ -355,7 +361,9 @@
355361
9878A1ED1B7876A70005ACD9 /* OpenWeatherMap.swift in Sources */,
356362
98BA27361B785EB00047549E /* ViewController.swift in Sources */,
357363
9878A1E81B78745C0005ACD9 /* WeatherFetcher.swift in Sources */,
364+
9878A1EF1B78930E0005ACD9 /* Networking.swift in Sources */,
358365
9878A1E71B78745C0005ACD9 /* City.swift in Sources */,
366+
9878A1F11B7893210005ACD9 /* Network.swift in Sources */,
359367
98BA27341B785EB00047549E /* AppDelegate.swift in Sources */,
360368
);
361369
runOnlyForDeploymentPostprocessing = 0;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//
2+
// Network.swift
3+
// SwinjectSimpleExample
4+
//
5+
// Created by Yoichi Tagaya on 8/10/15.
6+
// Copyright © 2015 Swinject Contributors. All rights reserved.
7+
//
8+
9+
import Foundation
10+
import Alamofire
11+
12+
struct Network : Networking {
13+
func request(response: NSData? -> ()) {
14+
Alamofire.request(.GET, OpenWeatherMap.url, parameters: OpenWeatherMap.parameters)
15+
.response { _, _, data, error in
16+
response(data)
17+
}
18+
}
19+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// Networking.swift
3+
// SwinjectSimpleExample
4+
//
5+
// Created by Yoichi Tagaya on 8/10/15.
6+
// Copyright © 2015 Swinject Contributors. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
protocol Networking {
12+
func request(response: NSData? -> ())
13+
}

SwinjectSimpleExample/WeatherFetcher.swift

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,16 @@ import Alamofire
1111
import SwiftyJSON
1212

1313
struct WeatherFetcher {
14-
static func fetch(response: [City]? -> ()) {
15-
Alamofire.request(.GET, OpenWeatherMap.url, parameters: OpenWeatherMap.parameters)
16-
.response { _, _, data, error in
17-
let cities = data.flatMap { decode($0) }
18-
response(cities)
19-
}
14+
let networking: Networking
15+
16+
func fetch(response: [City]? -> ()) {
17+
networking.request { data in
18+
let cities = data.flatMap { self.decode($0) }
19+
response(cities)
20+
}
2021
}
2122

22-
private static func decode(data: NSData) -> [City]? {
23+
private func decode(data: NSData) -> [City]? {
2324
let json = JSON(data: data)
2425
var cities = [City]()
2526
for (_, j) in json["list"] {

SwinjectSimpleExampleTests/WeatherFetcherSpec.swift

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +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-
WeatherFetcher.fetch { cities = $0 }
60+
let fetcher = container.resolve(WeatherFetcher.self)!
61+
fetcher.fetch { cities = $0 }
1962

2063
expect(cities).toEventuallyNot(beNil())
21-
expect(cities?.count).toEventually(equal(12))
22-
expect(cities?[0].id).toEventually(equal(6077243))
23-
expect(cities?[0].name).toEventually(equal("Montreal"))
24-
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"))
2577
}
2678
}
2779
}

0 commit comments

Comments
 (0)