[Office Hours] is there a way to mock dates in unit tests? #97
-
Related DiscussionIs there a way to properly mock dates in unit tests? I attempted to mock them as such:
but i think it may be causing race conditions since sometimes my tests pass and at other times they don't. i remember briefly reading that swift unit tests run concurrently, though i'm not certain. ReproductionN/A Additional contextNo response Code of Conduct
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
I'm guessing you have static dates for your prescription test data and trying to compare that with today? Option2: Best practice |
Beta Was this translation helpful? Give feedback.
-
Hi @kkellybonilla, Along the lines of what @ChenaLee has mentioned in her comment, there is a "simple" dependency injection mechanism you can use here to inject a function that generates the date, as described here. @PSchmiedmayer do you have any advice here? |
Beta Was this translation helpful? Give feedback.
Hi @ChenaLee,
Good question!
Here are multiple great dependency injection frameworks out there that you can use to simplify your testing setup. Pointfree has a gerat library that you can use (swift-dependencies).
Factory and Swinject are also great options.
For simpler elements you might also want to use simple default values or injecting elements in initializers if necessary or follow articles such as the ones that @vishnuravi has linked.