vendredi 26 mai 2017

How to use table-driven tests in Swift with class reflection

I have a lot of Realm data models that I want to test in a Quick spec. The mechanics of the test for each model is identical:

  1. Expect the realm to be empty for the object type
  2. Load sample data from a JSON file
  3. Init the model with the JSON data
  4. Save the model object to the realm
  5. Expect the realm to contain the loaded object

All the models inherit from the same BaseModel type.

Since the boilerplate code is the same I was hoping to use a table-driven approach to the tests using an array and class reflection like this:

let tests: [String] = [
    "Model1",
    "Model2",
    "Model3"
]
for testClass in tests {
    describe(testClass) {
        // Use reflection to get the model type, but really I don't want the base model type, I want the actual type since Realm needs it
        let modelType = NSClassFromString("MyApp.\(testClass)") as! BaseModel.Type
        // Test the model
        it("Adds good \(testClass) from json", closure: {
            expect(realm.objects(modelType).count).to(equal(0))

            if let json = self.loadJsonFromFile("event") {
                if let e = modelType.init(JSON: json) {
                    do {
                        try realm.write {
                            realm.add(e, update: true)
                        }
                    } catch {}
                }
            }

            expect(realm.objects(modelType).count).to(equal(1))

        })
    }
}

This fails because the BaseModel type isn't really want I want to instantiate. I want to instantiate the actual testClass type since I need to insert the correct model type into Realm for this to work. Also, the base model has several pieces undefined that Realm needs such as the primaryKey which is left to the descendent models to implement.

The concept of attempting to use reflection to get the actual model class is really separate from Realm so this isn't necessarily a Realm question. Since I don't know the model type in the loop except as the String value is there any way to get the actual model type rather than just the inherited parent type?





Aucun commentaire:

Enregistrer un commentaire