In this series, I show you how to unit test Core Data models. Before we start, though, I'd like to challenge you. How would you unit test a Core Data model? Pause the video for a moment and try to come up with a strategy. Remember what we covered in Core Data Fundamentals. Which ingredients do we need to create an NSManagedObject instance?
Remember from Core Data Fundamentals that we need a managed object context to instantiate a managed object. To freshen up your memory, this is what it takes to create a note.
It's time to write the first unit test for the Note class. The first unit test we write tests the updatedAtAsDate computed property of the Note class. Remember that we only unit test the behavior we add to the Note class. We name the unit test testUpdatedAtAsDate().
Let's start with the updatedAtAsDate computed property. Change the name of the unit test to testUpdatedAtAsDate_NotNil(). We need to implement a second unit test for the updatedAtAsDate computed property if the updatedAt property doesn't have a value.
It's time to shift gears and focus on the Category class. This shouldn't be difficult since you already know how to write unit tests for a Core Data model and we laid the foundation when we implemented the unit tests for the Note class. Pause the episode for a moment and give it a try.