将Swift文件添加到测试目标而不是修复单元测试 [英] Adding Swift files to test target not fixing unit tests

查看:191
本文介绍了将Swift文件添加到测试目标而不是修复单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我查看了很多博客,并尝试了许多方法让我的Swift单元测试在Xcode 6.0.1(或者6.1)中工作。我正在尝试访问我的应用程序目标中的类,所以我写了这一行:

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!

当然,我得到了使用未声明的类型'LoginViewController'错误。

Of course, I get the "Use of undeclared type 'LoginViewController'" error.

然后我尝试将 LoginViewController 添加到我的测试目标,但随后我在其他类中获得使用未解析的标识符错误我的项目。所以我尝试将这些类添加到我的测试目标中,但我最终得到了一个看似无穷无尽的错误来源,如下面的截图:

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:

将我的所有课程声明为公开,会导致其他错误,看起来像不好的做法。无论如何在Swift项目中包含单元测试依赖于许多框架和类?我只想从与文章。

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.

推荐答案

经过多次头痛并把它放在后面,我发现主要的问题是在向文件添加文件时测试目标成员资格,Objective-C类仍然会抱怨。我认为这是一个旧的兼容性问题,Swift进行单元测试的新方式,但解决方案是我的测试目标不知道有一个桥接标题。因此,我在测试目标的构建设置中添加了对它的引用,如下所示:

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:

现在看似简单明了,但错误无益。没有其他解决方案我看到Swift单元测试表明这可能是一个问题,但现在我知道了。

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;博士

对于在Swift中工作的单元测试,测试目标必须知道应用目标知道的所有内容,因此也要在测试目标中添加对桥接头的引用(如果适用)。

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

这篇关于将Swift文件添加到测试目标而不是修复单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆