mirror of
https://github.com/apple/swift-nio
synced 2026-11-25 04:47:49 +00:00
tests: improve tests that are supposed to throw (#1430)
Motivation:
The
do {
try someOperation()
XCTFail("should throw") // easy to forget
} catch error as SomethingError {
XCTAssertEqual(.something, error as? SomethingError)
} catch {
XCTFail("wrong error")
}
pattern is not only very long, it's also very error prone. If you forget
any of the XCTFails, you might not tests what it looks like
XCTAssertThrowsError(try someOperation) { error in
XCTAssertEqual(.something, error as? SomethingError)
}
is much safer and shorter.
Modifcations:
Do many of the above replaces.
Result:
Cleaner, shorter, and safer tests.
This commit is contained in:
1 parent
2512b8e277
commit
8912d1a48b
19 files changed
+282
-736
No files matched your search
@@ -141,15 +141,9 @@ class HTTPRequestEncoderTests: XCTestCase {
|
||||
}
|
||||
|
||||
private func assertOutboundContainsOnly(_ channel: EmbeddedChannel, _ expected: String) {
|
||||
do {
|
||||
if let buffer = try channel.readOutbound(as: ByteBuffer.self) {
|
||||
buffer.assertContainsOnly(expected)
|
||||
} else {
|
||||
fatalError("Could not read ByteBuffer from channel")
|
||||
}
|
||||
} catch {
|
||||
XCTFail("unexpected error: \(error)")
|
||||
}
|
||||
XCTAssertNoThrow(XCTAssertNotNil(try channel.readOutbound(as: ByteBuffer.self).map { buffer in
|
||||
buffer.assertContainsOnly(expected)
|
||||
}, "couldn't read ByteBuffer"))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user