What if you could test your app perfectly without depending on real-world signals or slow services?
Why Test doubles (mocks, stubs) in Swift? - Purpose & Use Cases
Imagine you are testing a car's navigation app that needs live GPS data and internet maps. Without test doubles, you must rely on real GPS signals and internet connections every time you test.
This manual way is slow and unreliable. If GPS is weak or internet is down, your tests fail even if your code is fine. Also, you can't easily test rare cases like wrong locations or no signal.
Test doubles like mocks and stubs act as pretend parts. They give fixed GPS data or fake internet responses instantly. This makes tests fast, reliable, and lets you check how your app handles all situations.
let location = realGPS.getLocation() let map = realInternet.fetchMap(location)
let location = stubGPS.getFixedLocation() let map = mockInternet.fetchFakeMap(location)
Test doubles let you test your code anytime, anywhere, and for every possible case without waiting or failing due to outside problems.
When building a weather app, you can use stubs to simulate sunny or rainy weather data, so you see how your app looks without waiting for actual weather changes.
Manual testing with real parts is slow and unreliable.
Test doubles replace real parts with pretend ones for fast, stable tests.
This helps test all scenarios easily and confidently.