Created
June 7, 2020 18:43
-
-
Save DhavalDobariya86/8e39db5625a3bb9f90fc8500bf6fbee7 to your computer and use it in GitHub Desktop.
MVVM-C Coordinator Unit Tests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import XCTest | |
@testable import MVVMCDemo | |
class CountryListCoordinatorTests: XCTestCase { | |
private let sut = CountryListCoordinator(requestManager: LocalRequestManager()) | |
func testStart() { | |
XCTAssertTrue(sut.start() is CountryListViewController) | |
} | |
func testDidSelectCountry() { | |
_ = sut.start() | |
let viewController = UIViewControllerMock() | |
sut.didSelect(country: dummyContry, in: viewController) | |
guard let navigationController = viewController.viewControllerToPresent as? UINavigationController else { | |
XCTFail("Presented ViewController must be embedded in UINavigationController.") | |
return | |
} | |
XCTAssertTrue(navigationController.viewControllers.first is CountryDetailViewController) | |
} | |
func testDidFailLoading() { | |
let viewController = sut.start() | |
UIApplication.shared.windows.first?.rootViewController = viewController | |
sut.didFailLoading(error: NSError(domain: "test", code: 100, userInfo: nil)) | |
guard let alert = viewController.presentedViewController as? UIAlertController else { | |
XCTFail("Presented ViewController must be UIAlertController.") | |
return | |
} | |
XCTAssertEqual(alert.title, "Error") | |
XCTAssertEqual(alert.message, "Could not search country for the keyword.") | |
XCTAssertEqual(alert.actions.count, 1) | |
XCTAssertEqual(alert.actions[0].title, "Ok") | |
} | |
} | |
private let jsonData: Data = """ | |
{ | |
"name": "British Indian Ocean Territory", | |
"capital": "Diego Garcia", | |
"region": "Africa", | |
"subregion": "Eastern Africa", | |
"timezones": [ | |
"UTC+06:00" | |
], | |
"borders": [], | |
"currencies": [ | |
{ | |
"code": "USD", | |
"name": "United States dollar", | |
"symbol": "$" | |
} | |
], | |
"languages": [ | |
{ | |
"iso639_1": "en", | |
"iso639_2": "eng", | |
"name": "English", | |
"nativeName": "English" | |
} | |
] | |
} | |
""".data(using: .utf8)! | |
private let dummyContry: Country = try! JSONParser().parseData(data: jsonData) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment