[英]How to test UIFontMetrics UIFont.TextStyle in XCTest
How can I test the given UIFont.TextStyle
for a UIFont
with a dynamic font size via UIFontMetrics
in XCTest
unit testing?如何在XCTest
单元测试中通过UIFont.TextStyle
为具有动态字体大小的UIFont
测试给定的UIFontMetrics
?
var myDynamicFont: UIFont {
let font = UIFont(name: "Font-Name", size: 42)
return UIFontMetrics(forTextStyle: .largeTitle).scaledFont(for: font)
}
XCTest Unit Test: XCTest 单元测试:
func testMyDynamicFont() {
...
XCTAssertEqual(myDynamicFont.???, .largeTitle) // <-- How to check the given TextStyle?
}
Thanks!谢谢!
You can use SnapshotTesting :您可以使用SnapshotTesting :
func test_myDynamicFont_matchesSnapshot() {
assertSnapshot(matching: myDynamicFont, as: .dump)
}
Snapshot with.dump strategy will produce something like that:带有.dump 策略的快照将产生类似的结果:
- <UICTFont> font-family: ".SFUI-Regular"; font-weight: normal; font-style: normal; font-size: 42.00pt
Or you can use your view to produce.image snapshot and match it:或者您可以使用您的视图来生成.image 快照并匹配它:
func test_label_matchesSnapshot() {
let label = UILabel()
label.text = "some text"
label.sizeToFit()
label.font = myDynamicFont
assertSnapshot(matching: label, as: .image, record: true)
}
This will produce image snapshot:这将产生图像快照:
and will match it every time you run tests.并且每次运行测试时都会匹配它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.