@@ -10,6 +10,7 @@ but also how effects are executed and feed data back into the system.
10
10
* [ Testing state changes] [ Testing-state-changes ]
11
11
* [ Testing effects] [ Testing-effects ]
12
12
* [ Non-exhaustive testing] [ Non-exhaustive-testing ]
13
+ * [ Testing gotchas] ( #Testing-gotchas )
13
14
14
15
## Testing state changes
15
16
@@ -553,6 +554,51 @@ The test still passes, and none of these notifications are test failures. They j
553
554
what things you are not explicitly asserting against, and can be useful to see when tracking down
554
555
bugs that happen in production but that aren't currently detected in tests.
555
556
557
+ ## Testing gotchas
558
+
559
+ This is not well known, but when an application target runs tests it actually boots up a simulator
560
+ and runs your actual application entry point in the simulator. This means while tests are running,
561
+ your application's code is separately also running. This can be a huge gotcha because it means you
562
+ may be unknowingly making network requests, tracking analytics, writing data to user defaults or
563
+ to the disk, and more.
564
+
565
+ This usually flies under the radar and you just won't know it's happening, which can be problematic.
566
+ But, once you start using this library and start controlling your dependencies, the problem can
567
+ surface in a very visible manner. Typically, when a dependency is used in a test context without
568
+ being overridden, a test failure occurs. This makes it possible for your test to pass successfully,
569
+ yet for some mysterious reason the test suite fails. This happens because the code in the _ app
570
+ host_ is now running in a test context, and accessing dependencies will cause test failures.
571
+
572
+ This only happens when running tests in a _ application target_ , that is, a target that is
573
+ specifically used to launch the application for a simulator or device. This does not happen when
574
+ running tests for frameworks or SPM libraries, which is yet another good reason to modularize
575
+ your code base.
576
+
577
+ However, if you aren't in a position to modularize your code base right now, there is a quick
578
+ fix. Our [ XCTest Dynamic Overlay] [ xctest-dynamic-overlay-gh ] library, which is transitively included
579
+ with this library, comes with a property you can check to see if tests are currently running. If
580
+ they are, you can omit the entire entry point of your application:
581
+
582
+ ``` swift
583
+ import SwiftUI
584
+ import XCTestDynamicOverlay
585
+
586
+ @main
587
+ struct MyApp : App {
588
+ var body: some Scene {
589
+ WindowGroup {
590
+ if ! _XCTIsTesting {
591
+ // Your real root view
592
+ }
593
+ }
594
+ }
595
+ }
596
+ ```
597
+
598
+ That will allow tests to run in the application target without your actual application code
599
+ interfering.
600
+
601
+ [ xctest-dynamic-overlay-gh ] : http://github.com/pointfreeco/xctest-dynamic-overlay
556
602
[ Testing-state-changes ] : #Testing-state-changes
557
603
[ Testing-effects ] : #Testing-effects
558
604
[ gh-combine-schedulers ] : http://github.com/pointfreeco/combine-schedulers
0 commit comments