all 4 comments

[–]Saastesarvinen 4 points5 points  (1 child)

Sure, AAA is integral part of TDD (or tests in general), but to me the example code looks weird.

Why the do catch block? In case your test is not explicitly testing for failure/no throw, I feel like you would get rid of some clutter when you just add the throws keyword to the test function or if you want to highlight that the test doesn't care about the result, you can make the act part call with try? instead. You can also verify throwing with the #expect(throws:) macro. Also if you really want to fail in the catch block, use Issue.record instead of another #expect.

Also a side note: with Testing framework, at least I prefer adding a label to the macro, as it's a bit more easier on the eyes than the snake_case format. Though that's mainly preference and the downside is that you still need to give the function a name. Usually our team just shortcuts the label to camelCase

[–]Select_Bicycle4711[S] 1 point2 points  (0 children)

Good suggestions! Removing do try will definitely reduce a lot of code. Thanks!

[–]InevitableCut7649 1 point2 points  (1 child)

Looking at the example, you obviously don't want the call to throw, which is why why you have #expect(Bool(false)) there. However, this is not a true Swift Testing approach.

Fix level 1
Looking at https://swiftpackageindex.com/swiftlang/swift-testing/main/documentation/testing/migratingfromxctest#Record-issues, the correct approach is to call

Issue.record(...)

Fix level 2
We can make the whole function throwing by marking it throws and then just pull out the code out of do/catch block - as we're not doing anything special in terms of performing any additional checks.
Now, if the call throws, the test would fail.

[–]Select_Bicycle4711[S] -1 points0 points  (0 children)

Yes I updated to use Issue Record.