Moya-stubber speeds up development and testing by adding a network stubbing screen to your app.
Want to add a stub response? It's as easy as creating a new JSON file in your project repository. Moya Stubber will automatically find it and make it activatable for stubbing.
Moya Stubber will:
Example of autogenerated stubbing panel
Swift Package Manager
.package(url: "https://github.com/wvteijlingen/moya-stubber.git", .upToNextMajor(from: "0.2.0"))
CocoaPods
pod "moya-stubber", :git => "https://github.com/wvteijlingen/moya-stubber.git"
Manually
Copy MoyaStubber.swift
and MoyaStubberViewController.swift
to your project.
endpointClosure
and stubClosure
:let stubsBundle = Bundle(url: Bundle.main.bundleURL.appendingPathComponent("Stubs.bundle"))!
try! MoyaStubber.shared.setStubsBundle(stubsBundle)
MoyaProvider(
endpointClosure: MoyaStubber.shared.endpointClosure,
stubClosure: MoyaStubber.shared.stubClosure
)
let viewController = MoyaStubberViewController(stubber: MoyaStubber.shared)
present(viewController, animated: true)
MoyaStubber expects the Stubs.bundle to have the following structure:
Moya.TargetType
enum.[name].[statusCode].[extension]
.If your Moya target looks like this…
enum MyMoyaTarget: TargetType {
case getUser
case getUserAvatar
case deleteUser
}
…your stubs bundle can be structured like this:
Stubs.bundle
| serverError.500.json
| notFound.404.json
|
| getUser
| | ok.200.json
|
| getUserAvatar
| | ok.200.jpg
|
| deleteUser
| | ok.204.json
If you don't want to include your stubs in release builds, you can exclude them from your target and use an Xcode Build Phase to include it only when needed. The exact command will depend on the project setup.
Example
if [ "${CONFIGURATION}" != "Release" ]; then
rsync --recursive --delete "${SRCROOT}/MyProject/Stubs.bundle" "${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.app"
fi
You can also enable or disable stubs programatically:
stubber.activate(stubNamed: "ok.200.json", forEndpointNamed: "getUser")
stubber.deactivateStub(forEndpointNamed: "getUser")
link |
Stars: 4 |
Last commit: 1 year ago |
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics