Android App Development
iOS App Development
Flutter App Development
Cross Platform App Development
Hire on-demand project developers and turn your idea into working reality.
Big thanks to Webkul and his team for helping get Opencart 126.96.36.199 release ready!
Updated 22 November 2019
Hello guys, in this blog we will perform unit testing on 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.
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.
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:
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.
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.
Your email address will not be published. Required fields are marked*
Save my name email and website in this browser for the next time I comment.
Be the first to comment.
Enquiry or Requirement
If you have more details or questions, you can reply to the received confirmation email.