Difference
更好的方法来识别两个实例之间的差异。
你写过测试吗?通常它们使用等式断言,例如 XCTAssertEqual
,如果对象不相等会发生什么?Xcode 会向你展示一屏的文字
这迫使你手动扫描文本并试图找出确切的问题,如果你可以只了解哪个属性不同呢?
安装
CocoaPods
将 pod 'Difference'
添加到您的 Podfile。
Carthage
将 github "krzysztofzablocki/Difference"
添加到您的 Cartfile。
SwiftPM
在您的Package manifest文件中添加以下依赖项:.package(name: "Difference", url: "https://github.com/krzysztofzablocki/Difference.git", .branch("master"))
使用lldb
输入以下命令以查看两个实例之间的差异:
po dumpDiff(expected, received)
与XCTest集成
将其添加到您的测试目标中
public func XCTAssertEqual<T: Equatable>(_ expected: @autoclosure () throws -> T, _ received: @autoclosure () throws -> T, file: StaticString = #filePath, line: UInt = #line) {
do {
let expected = try expected()
let received = try received()
XCTAssertTrue(expected == received, "Found difference for \n" + diff(expected, received).joined(separator: ", "), file: file, line: line)
}
catch {
XCTFail("Caught error while testing: \(error)", file: file, line: line)
}
}
如果您使用的是Xcode 11或更早的版本,将#filePath
替换为#file
。
与Quick集成
将其添加到您的测试目标中
public func equalDiff<T: Equatable>(_ expectedValue: T?) -> Predicate<T> {
return Predicate.define { actualExpression in
let receivedValue = try actualExpression.evaluate()
if receivedValue == nil {
var message = ExpectationMessage.fail("")
if let expectedValue = expectedValue {
message = ExpectationMessage.expectedCustomValueTo("equal <\(expectedValue)>", actual: "nil")
}
return PredicateResult(status: .fail, message: message)
}
if expectedValue == nil {
return PredicateResult(status: .fail, message: ExpectationMessage.fail("").appendedBeNilHint())
}
return PredicateResult(bool: receivedValue == expectedValue, message: ExpectationMessage.fail("Found difference for " + diff(expectedValue, receivedValue).joined(separator: ", ")))
}
}
编写以下代码以查看两个实例之间的差异:expect(received).to(equalDiff(expected))
与组合架构集成
如果您正在使用组合架构的nameLabels
配置以获取更适合减少器仪表化的差异,则可以使用此功能
diff(oldState, newState, indentationType: .pipe, nameLabels: .comparing)
您可以在基于以下代码的自己的ReducerInstrumentation
变体中使用此函数:ReducerInstrumentation code based on this
这样,您的差异将类似于以下内容
Received action:
AppAction.home(.howTo(.setSelectedSlide))
State:
home:
| selectedHowTo:
| | selectedSlide:
| | | Current: 8mnkni91h4fe
| | | Previous: exei4wpqsmdk