swiftmoyaquick-nimble

Moya stub request in BDD tests


I want to make a Moya stub request in my Quick/Nimble BDD tests. Moya has a sampleData parameter I created using a JSON file:

var sampleData: Data {
    switch self {
    case .getPlaces:
        // Provided that project have a file named get_places.json in it's bundle.
        guard let path = Bundle.main.path(forResource: "get_places", ofType: "json"),
            let data = Data(base64Encoded: path) else {
                return Data()
        }
        return data
    case .getPlaceDetail:
        // Provided that project have a file named get_place_detail.json in it's bundle.
        guard let path = Bundle.main.path(forResource: "get_place_detail", ofType: "json"),
            let data = Data(base64Encoded: path) else {
                return Data()
        }
        return data
    }
}

How can I use this parameter in tests? Any ideas to make a Moya stub request in tests?

Thank you!


Solution

  • Just use your provider like you did in your real code. Moya detects that current target is test target and will return the sample data instead of performing the request