Swiftpack.co - wvteijlingen/swift-network-mocker as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by wvteijlingen.
wvteijlingen/swift-network-mocker v0.2.0
Speed up development and testing by adding an autogenerated network stubbing panel to your app.
⭐️ 4
🕓 1 year ago
iOS macOS
.package(url: "https://github.com/wvteijlingen/swift-network-mocker.git", from: "v0.2.0")

moya-stubber: supercharged stubbing for Moya

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

index detail

Installation

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.

Usage

  1. Create a bundle containing your JSON stub responses. See Stubs.bundle file structure.
  2. Configure your MoyaProvider to use the stubber by adding an 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
)
  1. Add the stubbing view controller:
let viewController = MoyaStubberViewController(stubber: MoyaStubber.shared)
present(viewController, animated: true)

Stubs bundle file structure

MoyaStubber expects the Stubs.bundle to have the following structure:

  1. 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.
  2. Within each endpoint directory you can place multiple stubs with the following naming pattern [name].[statusCode].[extension].
  3. 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.

Example

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

Advanced

Exclude stubs from release builds

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

Enable stubs programatically

You can also enable or disable stubs programatically:

stubber.activate(stubNamed: "ok.200.json", forEndpointNamed: "getUser")
stubber.deactivateStub(forEndpointNamed: "getUser")

GitHub

link
Stars: 4
Last commit: 1 year ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Dependencies

Release Notes

moya-stubber 0.2.0
2 years ago
  1. Improved user interface for selecting stubs

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics