- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我知道
@testable import MyModule
提供从“测试”(使用“testTarget”构建)模块 MyModuleTests
探索 MyModule
的非公共(public)成员的能力。
我的“非测试”模块需要相同的功能。不在生产中,只是在 Debug模式下。
我的问题是:你知道怎么做吗?
相关的(我认为,更难的问题):@testable
背后究竟发生了什么魔法?
最佳答案
要回答您的问题,出于调试目的,您实际上可以使用它。假设您有一个工作区 MyAwesomeWkspace
和一个位于 MyAwesomeProject
中的项目。
现在,创建一个名为 MyAwesomeModule
的新 framework
也称为 module
。在该模块中创建一个名为 Person
的非公共(public)类。
如果您尝试通过执行 import MyAwesomeModule
然后像 let p = Person( )
你会遇到一个错误。
但是如果您执行 @testable import MyAwesomeModule
,奇迹就会发生,您现在可以使用该类了。
基本上 @testable
允许您测试您没有声明为公开的内容。如您所见,注释仅适用于 import
here .
所以为了工作,目标是用-enable-testing
编译的,这样你就可以访问非公共(public)成员。至少基于什么是here
因为默认情况下,debug
构建配置是使用 -enable-testing
编译的,我向您展示的示例将起作用。但是,如果您将构建配置更改为 release
,您将看到一条错误消息,提示 Module .. was not compiled for testing
since the release
配置不是用标志 build 的。
The Swift access control model, as described in the Access Control section of The Swift Programming Language (Swift 4), prevents an external entity from accessing anything declared as internal in an app or framework. By default, to be able to access these items from your test code, you would need to elevate their access level to at least public, reducing the benefits of Swift’s type safety.
Xcode provides a two-part solution to this problem:
When you set the Enable Testability build setting to Yes, which is true by default for test builds in new projects, Xcode includes the -enable-testing flag during compilation. This makes the Swift entities declared in the compiled module eligible for a higher level of access. When you add the @testable attribute to an import statement for a module compiled with testing enabled, you activate the elevated access for that module in that scope. Classes and class members marked as internal or public behave as if they were marked open. Other entities marked as internal act as if they were declared public.
更多here
后期编辑:swift 的一个很酷的部分是它是开源的。因此,如果您想深入了解“魔法”,请查看:https://github.com/apple/swift
关于ios - XCTest 的@testable 幕后发生了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46798225/
对于要执行的JS代码,由解析器逐行解析,如果代码无效,则显示错误信息。如果一切正确,那么解析器会生成一个称为抽象语法树的数据结构。然后使用此抽象语法树为解释器生成字节码以供执行。 以上快速分析可以总结
似乎如果我在 () 中包装一个字符串、 bool 值或数字原始值,我会得到一个包装原始值的字符串、 bool 值、数字对象。这个结论正确吗? 此外,似乎 () 对于字符串和 bool 值是可选的,但对
我有几个关于 Java 中的嵌套类的问题。 关于内存分配,嵌套类是如何“隐藏”的? 您不能在嵌套类中声明静态变量(我认为确切的错误是静态属性只能在顶级类中声明)。为什么会这样?嵌套类还有哪些其他限制?
对于没有使用 Lambda Expresstions 经验的人,下面的代码让它看起来很神奇: int totalScore = File.ReadLines(@"c:/names.txt")
一个朴素的类型系统会将对象存储为指向其类型的指针(其中包含许多有用的信息,如 vtable、对象大小等),然后是其数据。如果.Net 有这样的类型系统,object在 32 位系统上占用 4 个字节,
我有以下用于字符串加密和解密的JAVA代码: public class AES { private SecretKeySpec setKey(String myKey) {
我试图了解 React 中的“组件”。 我有几个问题,所以我认为社区是提出问题的最佳场所。 1 - 当我们这样做时: var foo = React.createClass ({ ... }); Co
我想知道 ref 和 out 关键字在幕后是如何工作的?例如,如果我们在方法上使用它,它会把这个值类型变量放入某个类中以便像使用引用类型一样使用它吗? 最佳答案 in order to work wi
我对 Rails ActiveRecord、Doctrine for PHP(以及类似的 ORM)背后的一些设计很感兴趣。 ORM 如何设法实现链式访问器等功能,它们通常需要多深的工作? ORM 如何
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: C# “as” cast vs classic cast 我想知道当我做类似的事情时,.Net CLR 的底
好吧,这似乎是一个菜鸟问题,但我认识的许多 Web 开发人员都没有完全理解这个问题。 基本上,如何使用 FileUpload 控件的上传事件将文件从网页文件输入框上传到网络服务器(例如托管 .net
我很熟悉,按下返回键会导致 activity 被“销毁”,或者当开发人员调用函数 finish() 时,或者当系统需要时内存等... 并且还熟悉我们需要在 onDestroy 中执行清理过程,例如 u
我正在使用 GameViewController 和 GameScene。这个链接到 GameScene.sks。在 GameViewController 中,我将 aspect radio 设置为
关于 EF 的另一个问题: 我想知道在遍历查询结果时幕后发生了什么。 例如,查看以下代码: var activeSources = from e in entitiesContext.Sources
你好,我有一个关于 d3 的性质的问题,我认为这是关于 d3 的非常深入的细节。 据我了解, d3 中的变量声明,如 var svg = d3.select('boby').append('svg'
我是一名优秀的程序员,十分优秀!