This repository was archived by the owner on Aug 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathNetworkSpec.swift
More file actions
70 lines (62 loc) · 2.8 KB
/
NetworkSpec.swift
File metadata and controls
70 lines (62 loc) · 2.8 KB
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
//
// NetworkSpec.swift
// SwinjectMVVMExample
//
// Created by Yoichi Tagaya on 8/22/15.
// Copyright © 2015 Swinject Contributors. All rights reserved.
//
import Quick
import Nimble
@testable import ExampleModel
/// Specificatios of Network.
/// As a stable network service, https://httpbin.org is used.
/// Refer to the website for its API details.
class NetworkSpec: QuickSpec {
override func spec() {
var network: Network!
beforeEach {
network = Network()
}
describe("JSON") {
it("eventually gets JSON data as specified with parameters.") {
var json: [String: Any]? = nil
network.requestJSON("https://httpbin.org/get", parameters: ["a": "b" as AnyObject, "x": "y" as AnyObject])
.on(value: { json = $0 as? [String: Any] })
.start()
expect(json).toEventuallyNot(beNil(), timeout: 5)
expect((json?["args"] as? [String: AnyObject])?["a"] as? String).toEventually(equal("b"), timeout: 5)
expect((json?["args"] as? [String: AnyObject])?["x"] as? String).toEventually(equal("y"), timeout: 5)
}
it("eventually gets an error if the network has a problem.") {
var error: NetworkError? = nil
network.requestJSON("https://not.existing.server.comm/get", parameters: ["a": "b" as AnyObject, "x": "y" as AnyObject])
.on(failed: { error = $0 })
.start()
expect(error).toEventually(equal(NetworkError.NotReachedServer), timeout: 5)
}
}
describe("Image") {
it("eventually gets an image.") {
var image: UIImage?
network.requestImage("https://httpbin.org/image/jpeg")
.on(value: { image = $0 })
.start()
expect(image).toEventuallyNot(beNil(), timeout: 5)
}
it("eventually gets an error if incorrect data for an image is returned.") {
var error: NetworkError?
network.requestImage("https://httpbin.org/get")
.on(failed: { error = $0 })
.start()
expect(error).toEventually(equal(NetworkError.IncorrectDataReturned), timeout: 5)
}
it("eventually gets an error if the network has a problem.") {
var error: NetworkError? = nil
network.requestImage("https://not.existing.server.comm/image/jpeg")
.on(failed: { error = $0 })
.start()
expect(error).toEventually(equal(NetworkError.NotReachedServer), timeout: 5)
}
}
}
}