I am still pretty new to programming, but my first app was recently approved and is now for sale on the App Store. My app uses Core Data and is written in Swift. After some initial difficulties, I decide to write the app without unit tests. Now I'd like to implement unit tests to prevent regression.
My managed objects are created inside class methods in my
NSManagedObject subclasses and use a global variable "context" which I declare in
AppDelegate to store the
NSManagedObjectContext created in Apple's default code. I know that global variables are generally discouraged but this approach makes the most sense to me, is easy to write, and hasn't let to any bugs or other issues inside the app itself. Unfortunately, this makes it difficult to unit test. I've tried several different approaches to creating a Core Data stack inside my test target, but I can't seem to get anything to work without rewriting lots of my app code. I really don't want to do that just to make it testable. I've considered using frameworks such as Quick/Nimble or Magical Record but I don't see how that would help my problem.
I did figure out a workaround, but I'm curious if people think this is a bad idea: I created a class in my primary target (non testing) called
TestingClass. In my first
viewDidAppear, I call the
TestingClass is written in strait Swift with no testing frameworks. I have several if statements which are supposed to be false. If they are true they add a string to an array. When the tests are complete, if the array count is > 0, it prints out the contents and aborts.
I am planning on using this until my update is ready to ship. When it is, I will disable the
TestingClass and remove my call to it. I'm curious if anyone has ever done anything like this. Should I just figure out a way to do tests the "right" way?