Unit Testing Swift Application Using Parse.com

I have a sample application using Parse.com as a back end and am now writing some unit tests. As the Parse documentation says, when I subclassed PFObject

I applied the initialize method:

override class func initialize() {
    struct Static {
        static var onceToken : dispatch_once_t = 0;
    }
    dispatch_once(&Static.onceToken) {
        self.registerSubclass()
    }
}

      

When I run my unit tests, my application crashes from the initialize method with the following error:

Application terminated due to uncaught exception "NSInternalInconsistencyException", Reason: "Tried registering both TargetName.MyObject and TargetNameTests.MyObject as a custom PFObject subclass of MyObject. Cannot determine a suitable class because it does not inherit from another.

Any idea how to fix this problem?

+3


source to share


1 answer


The only way I've found a way to make this work is to not include the subclass in the target audience, and to declare the subclass and its variables and methods publicly available. And also import the target application

#import MyProject
#import Parse
#import XCTest

public class MySubClass: PFObject, PFSubclassing {
     @NSManaged public var name: String?

     public static func parseClassName() -> String {
       return "MySubClass"
     }
}

      



Then, in your test file, you need to call the class as such:

let testObject = MyProject.MyClass()

      

+3


source







All Articles