我刚刚开始深入研究新的 Swift 测试框架,但遇到了特征方面的问题.enabled(if: Bool)
。
下面我的代码返回Instance member 'accessTokenIsNil' cannot be used on type 'AuthorizationTests'; did you mean to use a value of this type instead?
部分错误.enabled(if: accessTokenIsNil())
。
import Testing
@testable import mews
import SwiftUI
struct AuthorizationTests {
let accessTokenManager = AccessTokenManager()
func accessTokenIsNil() -> Bool {
accessTokenManager.token == nil
}
@Test("Get initial access token", .enabled(if: accessTokenIsNil()))
func getAccessTokenSucces() {
// continue test
}
}
下面是来自Apple 的官方文档,所以我不确定我做错了什么。
func allIngredientsAvailable(for food: Food) -> Bool { ... }
@Test(
"Can make sundaes",
.enabled(if: Season.current == .summer),
.enabled(if: allIngredientsAvailable(for: .sundae))
)
func makeSundae() async throws { ... }
问题是您无法在宏
self
中访问@Test
,如果您仔细查看 Apple 文档中的示例,您会发现它们也没有,因为它们没有类型内部的测试,因此用于的函数.enabled
和测试函数都是全局的。所以要么做同样的事情,不要使用
struct AuthorizationTests {}
或者使用静态属性和函数