0

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("json-data") {
                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?

davidethell
  • 11,708
  • 6
  • 43
  • 63
  • Note that I've made some attempts using Mirror.subjectType and it does correctly see the proper type rather than the superclass, but it won't let me cast a type to another type. – davidethell May 26 '17 at 10:53
  • how about using `unsafeBitCast` to force a cast from `BaseModel` to `Mirror.subjectType`? – jpsim May 30 '17 at 17:37
  • Looks like `unsafeBitCast` only works on an instance of an object, but in my case, I need to get the correct type _before_ creating an instance since the init will be different. – davidethell Jun 01 '17 at 11:47

0 Answers0