This repository has been archived by the owner on Jun 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ExampleAPISpec.swift
136 lines (114 loc) · 5.13 KB
/
ExampleAPISpec.swift
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import Foundation
import Quick
import Nimble
import RxCocoa
import RxSwift
import XCTest
@testable import RxCocoaNetworking
extension ExampleAPI: TargetType {
var sampleData: Data {
switch self {
case .deleteRating:
return JSONHelper.data(fromFile: "delete-movie-rating")
case .rate:
return JSONHelper.data(fromFile: "rate-movie")
case .reviews:
return JSONHelper.data(fromFile: "get-movie-reviews")
}
}
}
final class ExampleAPISpec: QuickSpec {
override func spec() {
describe("ExampleAPI") {
var disposeBag: DisposeBag!
typealias SUT = ExampleAPI
let provider = Provider<SUT>()
let stubbedProvider = Provider<SUT>(stubBehavior: .immediate(stub: .default))
typealias Response = SingleEvent<Data>
var response: BehaviorRelay<Response?>!
var stubbedResponse: BehaviorRelay<Response?>!
beforeEach {
response = BehaviorRelay<Response?>(value: nil)
stubbedResponse = BehaviorRelay<Response?>(value: nil)
disposeBag = DisposeBag()
}
func bindRequest(_ target: SUT, provider: Provider<SUT>, response: BehaviorRelay<Response?>) {
provider.request(target)
.materialize()
.asObservable()
.bind(to: response)
.disposed(by: disposeBag)
}
func bindRequest(_ target: SUT) {
bindRequest(target, provider: provider, response: response)
}
func bindStubbedRequest(_ target: SUT) {
bindRequest(target, provider: stubbedProvider, response: stubbedResponse)
}
describe("delete movie rating") {
func target(movieID: String) -> SUT {
return SUT.deleteRating(movieID: movieID)
}
it("succeeds for sample stub") {
bindStubbedRequest(target(movieID: "123"))
expect(stubbedResponse.value?.element).toEventuallyNot(beNil())
}
it("succeeds for valid movie ID") {
bindRequest(target(movieID: "123"))
expect(response.value?.element).toEventuallyNot(beNil())
}
it("fails for invalid movie ID") {
bindRequest(target(movieID: "abc"))
expect(response.value?.error).toEventuallyNot(beNil())
}
}
describe("rate movie") {
func target(movieID: String) -> SUT {
return SUT.rate(movieID: movieID, rating: 8.5)
}
it("succeeds for sample stub") {
bindStubbedRequest(target(movieID: "123"))
expect(stubbedResponse.value?.element).toEventuallyNot(beNil())
}
it("succeeds for valid movie ID") {
bindRequest(target(movieID: "123"))
expect(response.value?.element).toEventuallyNot(beNil())
}
it("fails for invalid movie ID") {
bindRequest(target(movieID: "abc"))
expect(response.value?.error).toEventuallyNot(beNil())
}
}
describe("get movie reviews") {
func target(movieID: String, page: Int) -> SUT {
return SUT.reviews(movieID: movieID, page: page)
}
it("succeeds for sample stub") {
bindStubbedRequest(target(movieID: "123", page: 1))
expect(stubbedResponse.value?.element).toEventuallyNot(beNil())
}
it("succeeds for valid movie ID and page") {
bindRequest(target(movieID: "123", page: 1))
expect(response.value?.element).toEventuallyNot(beNil())
}
it("fails for invalid movie ID") {
bindRequest(target(movieID: "abc", page: 1))
expect(response.value?.error).toEventuallyNot(beNil())
}
it("fails for invalid page") {
bindRequest(target(movieID: "123", page: -1))
expect(response.value?.error).toEventuallyNot(beNil())
}
}
}
}
}
private final class JSONHelper {
static func data(fromFile fileName: String) -> Data {
let bundle = Bundle(for: self)
// Nil-coalescing allows the resource to be found when testing with `swift test`:
let resourceURL = bundle.url(forResource: fileName, withExtension: "json") ??
bundle.resourceURL!.appendingPathComponent("../../../../../../wiremock/__files/\(fileName).json")
return try! Data(contentsOf: resourceURL)
}
}