https://github.com/vinhnx/unittestingnetwork
Demo on how to build a simple and testable network suite in Swift, using Mockingjay.
https://github.com/vinhnx/unittestingnetwork
cocoapods codable design generic mockingjay stub swift unittesting urlsession
Last synced: about 1 year ago
JSON representation
Demo on how to build a simple and testable network suite in Swift, using Mockingjay.
- Host: GitHub
- URL: https://github.com/vinhnx/unittestingnetwork
- Owner: vinhnx
- Created: 2018-08-20T08:48:22.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2019-08-13T08:21:25.000Z (almost 7 years ago)
- Last Synced: 2025-02-05T23:57:17.291Z (over 1 year ago)
- Topics: cocoapods, codable, design, generic, mockingjay, stub, swift, unittesting, urlsession
- Language: Swift
- Homepage:
- Size: 72.3 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# UnitTestingNetworkWithStub_Demo
### About
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.
0. Show how easy it is to design a model using `Codable` protocol 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. Check [`User`](https://github.com/vinhnx/UnitTestingNetworkWithStub_Demo/blob/master/UnitTestingNetworkWithStub_2/Model/User.swift) and [`Repository`](https://github.com/vinhnx/UnitTestingNetworkWithStub_Demo/blob/master/UnitTestingNetworkWithStub_2/Model/Repository.swift) model.
1. Show how to design a clean and generic [`NetworkRequest`](https://github.com/vinhnx/UnitTestingNetworkWithStub_Demo/blob/master/UnitTestingNetworkWithStub_2/Network/NetworkRequest.swift) protocol that taking care of constructing the endpoint and handle network request, then serialize the data response into model using `URLSession`. [`Conformers`](https://github.com/vinhnx/UnitTestingNetworkWithStub_Demo/blob/master/UnitTestingNetworkWithStub_2/Network/GithubUserRequest.swift) just need to conform to `NetworkRequest` protocol and take advantage of Swift default protocol implementation, it can be [customizable](https://github.com/vinhnx/iOS-notes/issues/47).
2. Write test cases for [NetworkRequest](https://github.com/vinhnx/UnitTestingNetworkWithStub_Demo/blob/master/UnitTestingNetworkWithStub_2Tests/NetworkRequestTests.swift) and our [model](https://github.com/vinhnx/UnitTestingNetworkWithStub_Demo/blob/master/UnitTestingNetworkWithStub_2Tests/UserTests.swift) using `XCTest` and `Mockingjay` for network stub.
### Usage
Either check [CodableSimple.playground](https://github.com/vinhnx/UnitTestingNetworkWithStub_Demo/tree/master/CodableSimple.playground) or check [separate files](https://github.com/vinhnx/UnitTestingNetworkWithStub_Demo/tree/master/UnitTestingNetworkWithStub_2)
### TODO
+ Note to self: learn about `URLProtocol` to write a DIY network stub to even not using `Mockingjay` 🤔
+ UI testing
### References
- 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/