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:
- Provide an autogenerated view controller to activate and deactivate stubs at runtime.
- Allow you to configure multiple stubbed responses per endpoint, so you can easilty test your code in multiple scenarios.
- Use regular files for stubbed responses (json, xml, or any other filetype).
- Allow you to activate stubs programatically to simulate network responses in unit tests.
- Define a clear hierarchical structure for your stubs.
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.
- Create a bundle containing your JSON stub responses. See Stubs.bundle file structure.
- Configure your MoyaProvider to use the stubber by adding an
endpointClosure
andstubClosure
:
let stubsBundle = Bundle(url: Bundle.main.bundleURL.appendingPathComponent("Stubs.bundle"))!
try! MoyaStubber.shared.setStubsBundle(stubsBundle)
MoyaProvider(
endpointClosure: MoyaStubber.shared.endpointClosure,
stubClosure: MoyaStubber.shared.stubClosure
)
- Add the stubbing view controller:
let viewController = MoyaStubberViewController(stubber: MoyaStubber.shared)
present(viewController, animated: true)
MoyaStubber expects the Stubs.bundle to have the following structure:
- Every endpoint that you want to stub must be a subdirectory of the bundle.
Its name must match a case in your
Moya.TargetType
enum. - Within each endpoint directory you can place multiple stubs with the following naming pattern
[name].[statusCode].[extension]
. - Stubs that are placed in the root of the bundle will be available to all endpoints. You can use this for generic responses such as internal server errors.
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")