What are mocking and stubbing techniques for Core ML in Swift?

Mocking and stubbing are important techniques in unit testing, especially when dealing with machine learning models in Core ML. By utilizing these techniques, developers can test their applications without relying on real models or external dependencies, thereby increasing test reliability and speed.

Mocking involves creating a simulated version of a class or object that can mimic its behavior. This is useful when testing how your code interacts with Core ML models without needing the actual model to be loaded.

Stubbing, on the other hand, refers to creating a controlled response for a method call, allowing you to specify what should be returned without executing the method's original implementation. This can be particularly useful for returning mock predictions from a model.

import CoreML
import Foundation

class MockModel: MLModel {
    func predict(input: MLFeatureProvider) throws -> MLFeatureProvider {
        // Return a mock prediction
        let output = try MLDictionaryFeatureProvider(dictionary: [
            "predictedLabel": "Mock label",
            "probability": 0.99
        ])
        return output
    }
}

// Usage of MockModel in a test
func testModelPrediction() {
    let mockModel = MockModel()
    let sampleInput: MLFeatureProvider = // create sample input for testing

    do {
        let prediction = try mockModel.predict(input: sampleInput)
        assert(prediction.featureValue(for: "predictedLabel")?.stringValue == "Mock label")
    } catch {
        print("Prediction error: \(error)")
    }
}

Core ML Swift Mocking Stubbing Unit Testing Machine Learning