Advertisement
Guest User

Untitled

a guest
Aug 6th, 2017
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. public protocol CloudNetworkService {
  2. func login(userName: String, password: String, success: ()->(), failure: (Error)->())
  3.  
  4. func loadPancakeHouses(success: @escaping ([PancakeHouse])->(),
  5. failure: @escaping (CloudNetworkError)->())
  6. }
  7.  
  8. public class CloudNetworkManager: CloudNetworkService {
  9. // 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:
  10. public private(set) var isAuthenticated = false
  11. public func login(userName: String, password: String,
  12. success: ()->(), failure: (Error)->()) {
  13. assertionFailure("login hasn't been implemented yet...!")
  14. }
  15. public func loadPancakeHouses(success: @escaping ([PancakeHouse])->(),
  16. failure: @escaping (CloudNetworkError)->()) {
  17. let delayTime = DispatchTime.now() + Double(Int64(2 * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC)
  18. DispatchQueue.main.asyncAfter(deadline: delayTime) { [weak self] in
  19. guard let strongSelf = self else { return }
  20. guard strongSelf.isAuthenticated else {
  21. failure(.notAuthenticated)
  22. return
  23. }
  24. let path = Bundle.main.path(forResource: "pancake_houses", ofType: "plist")!
  25. let array = NSArray(contentsOfFile: path) as! [[String : Any]]
  26. let pancakeHouses = PancakeHouse.from(array)
  27. success(pancakeHouses)
  28. }
  29. }
  30. }
  31.  
  32. @testable import StackReview
  33.  
  34. public class MockCloudNetworkService: CloudNetworkService {
  35. public lazy var pancakeHouses: [PancakeHouse] = {
  36. let bundle = Bundle(for: type(of: self))
  37. let path = bundle.path(forResource: "test_pancake_houses", ofType: "plist")!
  38. let array = NSArray(contentsOfFile: path)
  39. return PancakeHouse.from(array as! [[String: Any]])
  40. }()
  41.  
  42. public func login(userName: String, password: String, success: () -> (), failure: (Error) -> ()) {
  43. success()
  44. }
  45.  
  46. public func loadPancakeHouses(success: @escaping ([PancakeHouse]) -> (), failure: @escaping (CloudNetworkError) -> ()) {
  47. success(pancakeHouses)
  48. }
  49. }
  50.  
  51. class PancakeHouseCollectionIntegrationTests: XCTestCase {
  52.  
  53. var collection: PancakeHouseCollection!
  54.  
  55. override func setUp() {
  56. super.setUp()
  57. collection = PancakeHouseCollection()
  58. }
  59.  
  60. func testLoadPancakesFromCloudFails() {
  61. // given
  62. let expectation = self.expectation(description: "Expected load pancakes from cloud to fail")
  63. // when
  64. collection.loadPancakesFromCloud { (didReceiveData) in
  65. expectation.fulfill()
  66. XCTAssertFalse(didReceiveData)
  67. }
  68. // then
  69. waitForExpectations(timeout: 3, handler: nil)
  70. }
  71.  
  72. func testGivenMockCloudNetworkServiceLoadPancakesFromCloudSucceeds() {
  73. // given
  74. let mockCloudNetworkService = MockCloudNetworkService()
  75. collection._cloudNetworkManager = mockCloudNetworkService
  76.  
  77. let expacation = self.expectation(description: "Expected load pancakes from cloud to succeed")
  78.  
  79. // when
  80. collection.loadPancakesFromCloud { (didReceiveData) in
  81. expacation.fulfill()
  82. XCTAssert(didReceiveData)
  83. XCTAssertEqual(self.collection._pancakeHouses, mockCloudNetworkService.pancakeHouses)
  84. }
  85.  
  86. // then
  87. waitForExpectations(timeout: 0.1, handler: nil)
  88. }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement