-
Notifications
You must be signed in to change notification settings - Fork 0
/
CaretakerMocks.swift
66 lines (53 loc) · 1.5 KB
/
CaretakerMocks.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
//
// CaretakerMocks.swift
// ExampleProjectTests
//
// Created by Brian Gerstle on 7/16/19.
//
import XCTest
import Scout
@testable import ExampleProject
// MARK: Mocks
class MockAnimal : Animal, Mockable {
let mock = Mock()
func eat<T>(_ food: inout T?) {
let rawResult = try! mock.call.eat(food: food)
// Avoid using force casting/unwrapping with a mock, since failures will crash your tests.
// Instead, use guard + XCTFail to report casting failures as test failures.
guard let eatenFood = rawResult as? T? else {
XCTFail("Mock should have returned food or nil, got \(String(describing: rawResult))")
fatalError()
}
food = eatenFood
}
}
class MockFoodDepot: FoodDepot, Mockable {
let mock = Mock()
func get<T>() -> T {
guard let result = try! mock.call.get() as? T else {
XCTFail("Not expecting result of \(T.self)")
fatalError()
}
return result
}
func compost<T>(food: T) {
try! mock.call.compost(food: food)
}
}
// MARK: eat() Behaviors
func beEaten() -> FuncExpectationBlock {
return { _ in
return nil
}
}
func onlyEatTuna() -> FuncExpectationBlock {
return { args in
// Since we get args as untyped KeyValuePairs, we need to cast it back to the expected type.
let food = args.first?.value!
return food is Tuna ? nil : food
}
}
// MARK: Dummy Food Types
struct Kibble {}
struct MeowMix {}
struct Tuna {}