-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathPostListViewModelTests.swift
101 lines (81 loc) · 2.9 KB
/
PostListViewModelTests.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
//
// PostListViewModelTests.swift
// SwiftMVVMDemoTests
//
// Created by Rushi Sangani on 06/12/2023.
//
import XCTest
import Combine
import NetworkKit
@testable import SwiftMVVMDemo
final class PostListViewModelTests: XCTestCase {
var postListViewModel: PostListViewModel!
var postService: MockPostService!
private var cancellables: Set<AnyCancellable>!
override func setUpWithError() throws {
postService = MockPostService()
postListViewModel = PostListViewModel(postService: postService)
cancellables = []
}
override func tearDownWithError() throws {
postListViewModel = nil
postService = nil
cancellables = nil
}
func testPostViewModelReturnsPosts() async throws {
try await postListViewModel!.loadPosts()
let posts = postListViewModel!.posts
// verify count
XCTAssertEqual(posts.count, 100)
let first = try XCTUnwrap(posts.first)
XCTAssertEqual(first.userId, 1)
XCTAssertEqual(first.id, 1)
let last = try XCTUnwrap(posts.last)
XCTAssertEqual(last.title, "at nam consequatur ea labore ea harum")
XCTAssertEqual(last.body, "cupiditate quo est a modi nesciunt soluta\nipsa voluptas error itaque dicta in\nautem qui minus magnam et distinctio eum\naccusamus ratione error aut")
}
func testPostsProperty() throws {
let expectation = XCTestExpectation(description: "@Published posts count")
// initial state
XCTAssertTrue(postListViewModel!.posts.isEmpty)
postListViewModel!
.$posts
.dropFirst()
.sink(receiveValue: { posts in
// verify count and fullfil the expectation
XCTAssertEqual(posts.count, 100)
expectation.fulfill()
})
.store(in: &cancellables)
Task {
try await postListViewModel!.loadPosts()
}
wait(for: [expectation], timeout: 3)
}
func testPostViewModelFailedGettingPosts() async {
postService.shouldFail = true
do {
try await postListViewModel!.loadPosts()
XCTFail("PostListViewModel should throw error.")
}
catch RequestError.failed(let error) {
XCTAssertEqual(error, "No posts found.")
}
catch {
XCTFail("PostListViewModel should throw RequestError.failed")
}
}
}
// MARK: - Mock
class MockPostService: PostRetrievalService {
var shouldFail: Bool = false
func getPosts() async throws -> [Post] {
guard !shouldFail else {
throw RequestError.failed(description: "No posts found.")
}
return try Bundle.test.decodableObject(forResource: "posts", type: [Post].self)
}
func getPostById(_ id: Int) async throws -> Post? {
nil
}
}