This is a sample demo on how to build a simple and tested network suite using just vanila Foundation and the power of Swift programming language. Only to use Mockingjay third party library for network stub in unit test. This can be improved.
- Show how easy it is to design a model using
Codableprotocol from Swift 4+. So we no longer need to use any third party model mapping library, now that the Swift compiler will take care of it. CheckUserandRepositorymodel. - Show how to design a clean and generic
NetworkRequestprotocol that taking care of constructing the endpoint and handle network request, then serialize the data response into model usingURLSession.Conformersjust need to conform toNetworkRequestprotocol and take advantage of Swift default protocol implementation, it can be customizable. - Write test cases for NetworkRequest and our model using
XCTestandMockingjayfor network stub.
Either check CodableSimple.playground or check separate files
- Note to self: learn about
URLProtocolto write a DIY network stub to even not usingMockingjay🤔 - UI testing
- https://api.github.com/users/{user_name}
- https://developer.apple.com/documentation/foundation/archives_and_serialization/encoding_and_decoding_custom_types
- https://www.swiftbysundell.com/posts/writing-unit-tests-in-a-swift-playground
- http://hoangtran.me/ios/testing/2016/09/12/unit-test-network-layer-in-ios/
- https://useyourloaf.com/blog/swift-codable-with-custom-dates/