I have one scenario where I need to change value of Date in swift while doing XCUITest.
I mean if current date returns 2022/07/06 22:31, I just want it should use some different time while running UI test cases say 2022/07/06 24:31
Example:
let date = Date()
myLabel.text = date // 2022/07/06 22:31
While running UI test cases, if I swizzle Date and replace it with some other date by adding 2 hours.
So while executing UI test case,
myLabel.text = date // 2022/07/06 24:31
Date()
is you like), and pass the date rather than callingDate()
internally. (The main lesson is that you will need to restructure your code to assist in the testing; you won't be able to just reach in and monkeypatch the code to do random things.) – ReprieveClock
. Every place where you accessDate()
, you have an implicit dependency on the system time. Now you're trying to find a way to hack the system time to be different, and that's going to be hard. Instead, if you just made the time an explicit parameter (or a Clock that you can read the time from), then your tests could provide a fake time or a fake clock that vends fake times. – Elwell