Why do my Unit tests fail because my project has realm objects? - ios

My iOS app uses Realm. It has Realm subclasses like MyRealmClass.
When I try to run any unit test in my workspace, I get the following error:
"RLMObject subclasses with the same name cannot be included twice in the same target. Please make sure 'MyRealmClass' is only linked once to your current target."
What I tried:
* I tried removing 'MyRealmClass' from the Test target. That does not work. When I do that the code won't compile anymore because the code is dependent on 'MyRealmClass'

Related

Errors when add class UITest target

I have a simple weather app which gets data from OpenWeather API. I wanted to add UI tests to project. I added some classes to AppNameTests target in Target Memberships and after that I got many error in that class. But test is working and project build correctly and run without any problem. Is somebody know how to resolve this problem?
EDIT:
Your app's source code files should not be members of the unit test and UI test targets. To access your app's classes, structs, and functions in your tests, use the #testable import statement in your test classes.
#testable import AppName
Regarding Xcode showing a bunch of error messages when the project builds and the tests run without error, you can clean your build folder by choosing Product > Clean Build Folder, and see if that makes the error messages go away.

Adding unit tests for an existing objective c + swift project

I have an existing iOs project which I would like to add a unit test target to. The classes I'd like to test are both in objective c and swift.
I've managed to create a test target which allows me to test swift only code by adding the implementation swift files to the test target.
However, as soon as I import or use a class that imports objective c code, I run into Linker issues when building the test target:
...
Symbols not found for architecture x86_64
I've tried adding the objectivec mm files to my target which gets my passed the linker error, but I then get an unresolved identifier error for the class I'm importing.
I'm using xcode 9 and swift 3.
edit: I think this may have something to do with the fact the swift bridging header is not available in the test target, however I'm not sure how to add it.
Your test project is a separate target and should have all files it relies upon to be tested linked separately. So first of all click one of the .m files it's missing and check if the test project is also included in the targets. If this is the case there might be a problem with the bridging header your test project uses. Figure out which one it uses from the build settings of your target and see if it includes the same files as your main project.

Xcode Unit testing : class is implemented in both. one of the two will be used xctest

I am unable to access my app classes in xctest target. Getting "_OBJ_CLASS_$_MYClassName", reference from: objc-class-ref in TestsCase.m ld:Symbol(s) not found for acrtichecture i386. When I add .m file in test case build phase this error is removed but while running on test cases I am getting following warning - class is implemented in both. one of the two will be used. which is undefind xcode7 ios.
I have read many solution over stackoverflow saying to remove .m reference from test case target's build phase but if I do it, it gives me class reference error.
In this project I have added test case target later it was not exists earlier. Is this the reason that I need to do some build setting. I have tried all things which I found over stakoverflow nothing has solved my problem.
Everything is working fine if I create `new project' and try to access classes in the newely created project test case class.
I just got the reason and happy to share. Since I was adding test case target into an existing project and Enable Testability' option was set to NO for debug mode in my project's build setting. I set it toYES` and it solved my problem :-) Reference - stakeoverflow.

Adding Swift files to test target not fixing unit tests

I have looked at a lot of blogs and tried many things to get my Swift unit tests to work in Xcode 6.0.1 (or 6.1 for that matter). I'm trying to access classes in my app's target so I wrote this line:
var vc: LoginViewController!
Of course, I get the "Use of undeclared type 'LoginViewController'" error.
I then try to add LoginViewController to my test target, but then I get "use of unresolved identifier" errors on other classes in my project. So I try to add those classes to my test target, but I end up with a seemingly endless source of errors like the screenshot below:
Declaring all my classes as public, causes other errors and seems like bad practice. Is there anyway to include unit tests in a Swift project that relies on many frameworks and classes? I simply want to start with something almost exactly like the code in this article.
After much headache and putting this on the back burner, I found that the main problem was when adding files to the test target membership, the Objective-C classes would still complain. I figured it was an old compatibility issue with the new way Swift does unit tests, but the solution was my test target didn't know there was a bridging header. Thus, I added a reference to it in my test target's build settings, like so:
It seems simple and obvious now, but the errors were unhelpful. No other solutions I saw for Swift unit tests suggested this could be an issue, but now I know.
Tl;dr
For unit tests to work in Swift, the test target must know everything the app target knows, so add a reference to your bridging header in your test target too (if applicable).
If you're using Xcode 7, there's now a much better way of dealing with this using #testable import {ModuleName}.
Just make sure that the module you want to test has the build setting Enable Testability set to YES.
I am using Xcode 6.1
You need to add your swift file to the target membership of the test target

Link framework against App and Test Target

I have a custom Framework I use within my normal App target as well as the corresponding UnitTest target. Turns out that confuses the runtime in such way that it is unable to choose the correct implementation since it has multiple choices:
objc[35580]: Class AClass is implemented in both ../MyApp.app/MyApp and ../MyApp.app/MyAppTests. One of the two will be used. Which one is undefined.
That of course leads to weird behavior if you try to check an object's class hierarchy or do any other class related checks.
So it boils down to the following two questions:
I don't see similar logs for e.g. UIKit components, but this framework is also linked to both targets. Have I incorrectly compiled the framework?
Is it just a trivial configuration issue I missed?
PS: I already checked similar posts like 1 or 2, but although everything is configured as described, the problem remains.
You have added the dependency framework to the Tests target. This is flawed thinking. Since your primary application ALSO exports the SAME framework you will receive warnings about duplicated symbols for any classes found in the framework.
By removing your framework from the test target you can resolve the warnings. Remember, you're not losing any functionality by not linking against the same framework in the test target. Trust me, your code is still there.
I ran into a similar problem here: Xcode5: creating new testing target
The key is to create a new unit testing bundle, point it to your original target, and then don't do anything else! If you start including frameworks and source files into the test target, it'll generate these linking errors. The test target is supposed to "inject" the test classes into the actual target, not create a new separate target on it's own. So you just need to import the header files in your test class, and write your test cases.
I think the bundle should only "read" the framework's header files but not build the sources and leave that task to the App (remove the Framework .m files from the UnitTest target).
Right now the App and the UnitTest are both building the Framework, thus the duplicated classes.

Resources