Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public protocol CloudNetworkService {
- func login(userName: String, password: String, success: ()->(), failure: (Error)->())
- func loadPancakeHouses(success: @escaping ([PancakeHouse])->(),
- failure: @escaping (CloudNetworkError)->())
- }
- public class CloudNetworkManager: CloudNetworkService {
- // You can make the structure’s numberOfEdits property getter public, and its property setter private, by combining the public and private(set) access-level modifiers:
- public private(set) var isAuthenticated = false
- public func login(userName: String, password: String,
- success: ()->(), failure: (Error)->()) {
- assertionFailure("login hasn't been implemented yet...!")
- }
- public func loadPancakeHouses(success: @escaping ([PancakeHouse])->(),
- failure: @escaping (CloudNetworkError)->()) {
- let delayTime = DispatchTime.now() + Double(Int64(2 * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC)
- DispatchQueue.main.asyncAfter(deadline: delayTime) { [weak self] in
- guard let strongSelf = self else { return }
- guard strongSelf.isAuthenticated else {
- failure(.notAuthenticated)
- return
- }
- let path = Bundle.main.path(forResource: "pancake_houses", ofType: "plist")!
- let array = NSArray(contentsOfFile: path) as! [[String : Any]]
- let pancakeHouses = PancakeHouse.from(array)
- success(pancakeHouses)
- }
- }
- }
- @testable import StackReview
- public class MockCloudNetworkService: CloudNetworkService {
- public lazy var pancakeHouses: [PancakeHouse] = {
- let bundle = Bundle(for: type(of: self))
- let path = bundle.path(forResource: "test_pancake_houses", ofType: "plist")!
- let array = NSArray(contentsOfFile: path)
- return PancakeHouse.from(array as! [[String: Any]])
- }()
- public func login(userName: String, password: String, success: () -> (), failure: (Error) -> ()) {
- success()
- }
- public func loadPancakeHouses(success: @escaping ([PancakeHouse]) -> (), failure: @escaping (CloudNetworkError) -> ()) {
- success(pancakeHouses)
- }
- }
- class PancakeHouseCollectionIntegrationTests: XCTestCase {
- var collection: PancakeHouseCollection!
- override func setUp() {
- super.setUp()
- collection = PancakeHouseCollection()
- }
- func testLoadPancakesFromCloudFails() {
- // given
- let expectation = self.expectation(description: "Expected load pancakes from cloud to fail")
- // when
- collection.loadPancakesFromCloud { (didReceiveData) in
- expectation.fulfill()
- XCTAssertFalse(didReceiveData)
- }
- // then
- waitForExpectations(timeout: 3, handler: nil)
- }
- func testGivenMockCloudNetworkServiceLoadPancakesFromCloudSucceeds() {
- // given
- let mockCloudNetworkService = MockCloudNetworkService()
- collection._cloudNetworkManager = mockCloudNetworkService
- let expacation = self.expectation(description: "Expected load pancakes from cloud to succeed")
- // when
- collection.loadPancakesFromCloud { (didReceiveData) in
- expacation.fulfill()
- XCTAssert(didReceiveData)
- XCTAssertEqual(self.collection._pancakeHouses, mockCloudNetworkService.pancakeHouses)
- }
- // then
- waitForExpectations(timeout: 0.1, handler: nil)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement