Can't import main swift classes into test target?

后端 未结 4 982
半阙折子戏
半阙折子戏 2020-12-28 17:47

I am trying to test the classes in my iOS app. I am trying to import the target Pickle in my app that has all my classes into my testing target PickleTest

相关标签:
4条回答
  • 2020-12-28 18:05

    You don't have to do this. You can use link_with in your Podfile. In your case you would do.

    link_with 'Pickle', 'PickleTests'
    

    And then pod update

    0 讨论(0)
  • 2020-12-28 18:16

    Many thanks to @matt for helping me with this one!

    Right click on the images and open them in a new tab to see them larger.

    As discussed on https://github.com/CocoaPods/CocoaPods/issues/2695 the issue seemed to be lying with having Cocoapods as part of the project. The answer near the bottom of the link that solves the issue involves clicking on your Application settings, and then clicking on your Project Info (not any of your targets' settings). There you will see your Configurations settings: enter image description here

    You will also notice that there are two targets in the config settings, and that your Testing Target config settings will be set to none while your Main Target will be linked to the Cocoapods. The fix lies in changing that None value next to the Testing Target to the same thing that the Main Target has: enter image description here

    So now they are both linked to the Cocoapods. Making sure your main target has Defines module set to Yes in its Build Settings, build your project and the error in the testing files should go away.

    Also, after I did this fix I encountered another error where the linker was throwing an error complaining about missing a library in my testing target. This missing library was another dependency I had in my project, and I solved this error by making sure all of my dependencies that were linked in my Main Target's Link Binary with Libraries in its Build Phases settings were copied over to the Testing Target's Link Binary with Libraries in its Build Phases settings.

    0 讨论(0)
  • 2020-12-28 18:22

    Import Module on top of your test class

    @testable import myModuleName
    

    You can find-out your module name on Target->Build Settings-> Product Module Name

    Then you can use any of class which belongs to that module. No need to import classes one by one.

    0 讨论(0)
  • 2020-12-28 18:26

    FWIW, on Xcode 7, import with the annotation: @testable. Example:

    import Pickle
    @ImportTests
    

    Then you'll be able to see the classes in code completion and compiling. source: https://www.natashatherobot.com/swift-2-xcode-7-unit-testing-access/

    0 讨论(0)
提交回复
热议问题