Adding Swift files to test target not fixing unit tests

本小妞迷上赌 提交于 2019-11-26 20:34:49

问题


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.


回答1:


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).




回答2:


I am using Xcode 6.1

You need to add your swift file to the target membership of the test target




回答3:


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.



来源:https://stackoverflow.com/questions/26683209/adding-swift-files-to-test-target-not-fixing-unit-tests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!