0

How to write unit test cases for core data in swift4?

I am new to writing unit test cases, to understand core data unit test cases, I referred this link.

But while running unit test cases, If I select unit test cases at target membership (in "appDelegate" and "ToDoStorgeManager" files) then I am running unit test cases, it showing errors like "One of the two will be used. Which one is undefined." at same time app got crush. If I unselected unit test then its working fine as we expected.

Please look into below pictures at "Target Membership" section to get clarity about my question.

when I am writing unit test cases for independent classes, I am not facing above issue.But In core data I am facing issue with check and uncheck for unit cases in Target Membership.

Please help me to understand the above scenario.

enter image description here enter image description here

4
  • I'm not sure what your question is about? Is it unit testing or Core Data that is the issue, could you share the code that is giving you a problem. Also, if you are new to writing unit tests then why start with Core Data instead of writing test for some independent class/struct Commented Aug 30, 2018 at 17:56
  • I understood how to write unit test cases for independent class and struct but same way I am trying to write unit test case for core data, here I am getting above scenario what I mentioned in question. Commented Aug 31, 2018 at 4:46
  • Ok I understand but you still need to clarify your question as I mentioned in the first part of my comment, Commented Aug 31, 2018 at 6:46
  • I have edited my question, Please look into my question once again to get clarity about question. Commented Aug 31, 2018 at 7:39

1 Answer 1

1

When you turn on check box in Target Membership section you also add this file as Compile Source for unit test target:

Check box for ViewController is off

Check box for ViewController is off

Check box for ViewController is on

Check box for ViewController is on

All unit tests have target dependency because their role is to test classes from this dependency that contains ViewController as Compile Source also. Compiler doesn't know which reference to this file (from unit test or from target dependency) should be compiled. It is known as name collision.

You can access directly to classes from dependency target without adding one to unit test target.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.