Swift、访问修饰符和单元测试 [英] Swift, access modifiers and unit testing

查看:28
本文介绍了Swift、访问修饰符和单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚升级到 Xcode 6 beta 4,其中 Swift 编译器现在支持 访问修饰符.

I just upgraded to Xcode 6 beta 4, where the Swift compiler now supports access modifiers.

这给我带来了一个问题,因为我的单元测试现在无法编译(由于类不是公开的).

That caused a problem for me, since my unit tests now fail to compile (due to the classes not being public).

简单的解决方案当然是将所有经过测试的类都公开,但这感觉就像一个黑客(我个人的偏好是即使在非公开类上也编写单元测试).

The simple solution is of course to make all tested classes public, but that feels like a hack (my personal preference is to write unit tests even on non-public classes).

在 .NET 和 Java 中,您通常可以允许单元测试程序集级别(或 Java/OSGi 中的包级别)从单元测试程序集访问被测程序集.我不明白如何在 Swift 中做类似的事情.我真的必须公开我的所有课程才能对它们进行单元测试吗?

In .NET and Java, you can normally allow unit tests assembly-level (or bundle-level in Java/OSGi) access to the assembly under test from the unit test assembly. I did not understand how to do something similar in Swift. Do I really have to make all my classes public to unit test them?

推荐答案

这是一个已知问题,在 Beta 4 发行说明中提到.在提供更多信息之前,您可能希望推迟更改设计.

This is a known issue and mentioned in the Beta 4 release notes. You might want to hold off changing your designs until more information is provided.

我们知道我们的访问控制设计不适合单元测试(这在发行说明中),我们正在评估情况以了解我们能做些什么.

We're aware that our access control design isn't great for unit testing (and this was in the release notes), we're evaluating the situation to see what we can do.

--克里斯·拉特纳

访问控制系统的一个限制是单元测试不能与应用程序中的类和方法交互,除非它们被标记为公共.这是因为单元测试目标不是应用程序模块的一部分.

A limitation of the access control system is that unit tests cannot interact with the classes and methods in an application unless they are marked public. This is because the unit test target is not part of the application module.

-- Xcode beta 4 发行说明

-- Xcode beta 4 release notes

https://github.com/ksm/SwiftInFlux#当前访问控制设计的限制

这篇关于Swift、访问修饰符和单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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