我正在尝试为我的项目创建一个Swift包,其中包含一些共享的UI元素,我想为它编写一些测试
这是其中一种观点
//
// TextField.swift
// Test
//
// Created by Development on 05/03/2021.
//
#if !os(macOS)
import Combine
import SwiftUI
import UIKit
public struct TextFieldRepresentable: UIViewRepresentable {
@Binding var text: String
@Environment(\.isSecureField) var isSecureField: Bool
@Environment(\.isFirstResponder) var isFirstResponder: Binding<Bool>?
@Environment(\.placeholder) var placeholder: String?
@Environment(\.keyboardType) var keyboardType: UIKeyboardType
@Environment(\.returnKeyType) var returnKeyType: UIReturnKeyType
@Environment(\.textContentType) var contentType: UITextContentType?
@Environment(\.autoCapitalisation) var autoCapitalisation: Bool
@Environment(\.autoCorrection) var autoCorrection: Bool
@Environment(\.font) var font: Font?
let onCommit: (() -> Bool)?
public init( text: Binding<String>, onCommit: (() -> Bool)? = nil ) {
_text = text
self.onCommit = onCommit
}
public func makeUIView(context: Context) -> UITextField {
let textField = UITextField()
textField.delegate = context.coordinator
syncValues(context: context, textField: textField)
return textField
}
public func updateUIView(_ uiView: UITextField, context: Context) {
syncValues(context: context, textField: uiView)
DispatchQueue.main.async {
if let isFirstResponderValue = isFirstResponder?.wrappedValue {
switch isFirstResponderValue {
case true:
uiView.becomeFirstResponder()
case false:
uiView.resignFirstResponder()
}
}
}
}
private func syncValues(context _: Context, textField: UITextField) {
textField.text = text
textField.isSecureTextEntry = isSecureField
textField.textContentType = contentType
textField.keyboardType = keyboardType
textField.placeholder = placeholder
textField.font = UIFont.preferredFont(from: font)
textField.autocapitalizationType = autoCapitalisation ? .sentences : .none
textField.returnKeyType = returnKeyType
textField.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
textField.autocorrectionType = autoCorrection ? .yes : .no
}
public func makeCoordinator() -> Coordinator {
return Coordinator(parent: self)
}
public class Coordinator: NSObject, UITextFieldDelegate {
private let parent: TextFieldRepresentable
init(parent: TextFieldRepresentable) {
self.parent = parent
}
public func textFieldShouldReturn(_: UITextField) -> Bool {
return parent.onCommit?() ?? true
}
public func textFieldDidChangeSelection(_ textField: UITextField) {
parent.$text.wrappedValue = textField.text ?? ""
}
public func textFieldDidBeginEditing(_: UITextField) {
parent.isFirstResponder?.wrappedValue = true
}
}
}
#endif
我想测试一下,当我应用一个修改器时,底层的UIView会对更改做出响应。有没有一种方法可以在SPM单元测试中加载这个视图,并获得底层的extField,这样我就可以看到修饰符工作了?
我尝试使用Introspect和ViewInpsector,但没有运气。
我还注意到,在SPM中,即使我构造了视图,makeUIView(上下文:上下文)->扩展extField和**updateUIView(_ uiView:下一个字段,上下文:* 函数永远不会被调用。我在一个iOS项目单元测试中尝试了同样的方法,函数被正确调用。检查下面的代码
import Foundation
import XCTest
import SwiftUI
@testable import UI
final class TextFieldRepresentableTests: XCTestCase {
var view:TextFieldRepresentable!
var controller:UIHostingController<TextFieldRepresentable>!
override func setUp() {
view = TextFieldRepresentable(text: .constant("Test"))
controller = UIHostingController(rootView: view)
controller.loadViewIfNeeded()
let window = UIWindow(frame: UIScreen.main.bounds)
window.rootViewController = controller
window.makeKeyAndVisible()
}
func test_isSecureField_IsTrue_ShouldTextFieldBeSecure() {
view.makeUIView(context: UIViewRepresentableContext<TextFieldRepresentable>())
}
}
1条答案
按热度按时间new9mtju1#
测试
UIViewRepresentable
创建的子视图需要示例化一个在模拟器示例(或设备)上下文中运行的UIWindow
。要实现上述操作,您需要有一个iOS应用程序作为单元测试的主机。不幸的是,在这一点上,SPM不提供一个选项来创建iOS应用程序(不知道这将是如何可行的添加)。
变通办法是:
1.创建一个虚拟的iOS应用程序
1.为该iOS应用程序创建测试目标
1.将包链接到测试目标,这假设您在包中构建了库目标而不是测试目标
您可以将包添加为本地包,并将项目包含在源代码控制中(例如,git)。这样 checkout 包的人也可以运行测试。
但是,您可能需要重新考虑您的设计测试策略。例如,您可以将所有
UITextField
逻辑提取到一个没有任何SwiftUI依赖项的适配器类中,然后测试该类。这将导致更好和更快的测试。