Skip to content

Debugging with Voodoo

Derek Clarkson edited this page Nov 21, 2022 · 2 revisions

When you have a mock server available it's often useful to be able to run it in the background whilst debugging your app. If you are running a YAML configuration (ie. a typical non-XCTest setup) then it's a simple matter of just starting the server from the command line.

But what if you've setup your server for XCTest with a Swift configuration. That's a little more complex and one option is to do this:

Step 1: Create an app target

In Xcode add a new target to your project. Select 'MacOS - Command Line Tool' as the template. Call it whatever you like. For this example I'll call it MockServer.

Step 2: dependencies

The only dependency you will need for this setup is Voodoo. So add it as a dependency of the command line tool target.

Step 3: Adding sources

For this setup you'll need to ensure that the setup you are using in your XCTest is available to the mock server target. Perhaps the easiest way to do that is to ensure that the setup is configured through some static methods on an enum in in a seperate file from your tests. Then you can simply go to that file in Xcode, and select the command line target as also using the file. For example, lets say we have a file that looks like this:

Endpoints.swift

import Foundation
import Voodoo

public enum Endpoints {
    
    public static var config: HTTPEndpoint {
        HTTPEndpoint(.GET, "/app/config", response: .ok(body: .json([
            "version": 1.0,
            "featureFlag": true,
        ])))
    }

    public static var bookSearch: [Endpoint] {

        return [
            HTTPEndpoint(.GET, "/books", response: .ok(body: .json([book1, book2, book3]))),
            HTTPEndpoint(.GET, "/book/:bookId", response: .dynamic { request, _ in
                // return some end point.
            }),
        ]
    }

    public static var cart: [Endpoint] {
       // A bunch of cart end points.
    }
}

Step 4: Configuring the server

Now all we need to do is configure the command line code. With our example endpoints it might look like this:

import Foundation
import Voodoo

@main
struct MyApplication {
    static func main() throws {
        let server = try VoodooServer(verbose: true) {
            Endpoints.config
            Endpoints.bookSearch
            Endpoints.cart
        }
        server.wait()
    }
}

Step 5: Run

Now you can simply run the mock server target in Xcode to start the server and pass it's URL using the same launch argument you use in testing as a launch argument in the Xcode scheme for your app.