Unit Testing With Core Data

Updated 22 November 2019

Save

Hello guys, in this blog we will perform unit testing on core data.

Core Data

It is an object graph and persistence framework provided by Apple which is use to manage the model layer objects in your application.

The Core Data stack handles all of the interactions with the external data store and consists of 3 primary tools:

Managed Object Model,

It describe the schema that you will be using in your application.

Persistent Store Coordinator

It verifies that the data is in a consistent state that matches the definition on the model layer and is responsible for extracting instances of entities from that data.

Managed Object Context.

It is responsible for managing objects created and  returned using Core Data. It operates in-memory, not only for speed, but also to keep your model objects’ attributes updated across your application.

 

Unit Testing

They are automated tests that run and validate a piece of code to make sure it behaves as intended and meets its design.

They have their own target in Xcode and are written using the XCTest Framework. A sub class of XCTestCase contains test methods to run in which only the methods starting with “test” will be parsed by Xcode and available to run.

A useful test meet 3 criteria:

  1. The test must be able to fail
  2. The test must be able to pass
  3. The test must be refactored and kept simple.

Let’s dive into code and see how unit testing is performed.

First Add Unit Testing target in your project which contain Core Data implementation.

Now You have to enter name of you target file and click Finish.

Now we are going to write some test case for our Core Data model.

Note:- We have implemented Core Data logic using singleton Pattern. If you want to know more about singleton pattern. Follow this link.

In the above CoreDataTests is my class name. XCTestCase provides us two default methods which run with every test cases and those methods are listed above:

  1. setUp()
  2. tearDown()

setUp() call before running the test case. This method is best place for initialising properties and allocating resources which is used in your.

tearDown() call after the test method execution is complete.

Now we are going to write some test cases.

In the above code you can see that, in setUp() method we are initialising connection with core data and in tearDown() we are de-initialising connection with core data. These steps will follow for all test cases.

In test_create_data() we are testing create operation in Core data.

In test_remove_data() we are testing delete operation in Core data.

In test_fetch_all_data() we are testing fetching operation in Core data.

In test_save() we are saving data in Core data.

Now we will run test cases for checking status of unit testing.

Press Window + U to run all test cases or if you want to run particular test case then click on diamond icon near func keyword of test methods.

 

Conclusion

We should preform Unit testing because it is one of the earliest testing efforts performed on the code and the earlier defects are detected, the easier they are to fix.

author
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Start a Project


    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home