@@ -11,19 +11,6 @@ alwaysApply: true
11
11
### DO:
12
12
- Create separate test methods for each test case
13
13
- Use `guard` statements with `XCTFail` for unwrapping optionals
14
- - Write focused tests that test one thing at a time
15
- - Use descriptive test method names that explain what is being tested
16
- - Catch only one error type per test
17
- - Create separate test methods instead of conditional logic
18
-
19
- ### DON'T:
20
- - Use typed catches when testing throwing expressions
21
- - Delete and recreate test files when debugging
22
- - Add boilerplate comments like "// Given", "// When", "// Then"
23
-
24
- ## Code Examples
25
-
26
- ### ✅ CORRECT: Unwrapping optionals
27
14
```swift
28
15
func testSomething() {
29
16
guard let result = someOptionalValue else {
@@ -33,36 +20,61 @@ func testSomething() {
33
20
XCTAssertEqual(result, expectedValue)
34
21
}
35
22
```
23
+ - Write focused tests that test one thing at a time
24
+ - Use descriptive test method names in the format `test_<methodName>_<withCircumstances>_<shouldExpectation>`
25
+ ```
26
+ func test_canTransition_fromAppleSheetPresentedState_shouldAllowPaymentAuthorizationAndInterruptAndCompleted()
27
+ func test_ensureCurrencyNotChanged_withNoInitialCurrency_shouldNotThrow()
28
+ ```
36
29
37
- ### ❌ INCORRECT: Conditional test logic
38
- ```swift
39
- // DON'T DO THIS
40
- func testSomething() {
41
- if condition {
42
- XCTAssertTrue(something)
43
- } else {
44
- XCTAssertFalse(something)
30
+ - If a function may throw multiple types of errors, write multiple tests to capture them in isolation
31
+ ```
32
+ func throwingFunction() {
33
+ if someCondition {
34
+ throw Error.foo
35
+ } else
36
+ throw Error.bar
37
+ }
38
+
39
+ func test_throwingFunction_whenSomeConditionTrue_shouldThrowFoo() {
40
+ do {
41
+ _ = try await storefront.createCart()
42
+ XCTFail("Expected error to be thrown")
43
+ } catch {
44
+ guard case let error = Error.foo else {
45
+ XCTFail("Expected .foo")
46
+ }
45
47
}
46
- }
48
+ }
49
+ func test_throwingFunction_whenSomeConditionTrue_shouldThrowBar(){
50
+ do {
51
+ _ = try await storefront.createCart()
52
+ XCTFail("Expected error to be thrown")
53
+ } catch {
54
+ guard case let error = Error.foo else {
55
+ XCTFail("Expected .foo")
56
+ }
57
+ }
58
+ }
47
59
```
48
60
49
- ### ✅ CORRECT: Separate test methods
50
- ```swift
51
- func testSomething_whenConditionTrue() {
52
- // Test for true condition
53
- }
61
+ ## Code Examples
54
62
55
- func testSomething_whenConditionFalse() {
56
- // Test for false condition
63
+ ### ✅ CORRECT: Unwrapping optionals
64
+ ```swift
65
+ func testSomething() {
66
+ guard let result = someOptionalValue else {
67
+ XCTFail("Expected non-nil value")
68
+ return
69
+ }
70
+ XCTAssertEqual(result, expectedValue)
57
71
}
58
72
```
59
73
60
- ## Error Testing
61
-
62
- When testing throwing functions:
63
- - Test only ONE error type per test method
64
- - Avoid typed catch blocks that check for specific error types
65
- - Let unhandled errors fail the test naturally
74
+ ### DON'T:
75
+ - Use typed catches when testing throwing expressions
76
+ - Delete and recreate test files when debugging
77
+ - Add boilerplate comments like "// Given", "// When", "// Then"
66
78
67
79
## Comments
68
80
0 commit comments