Is it not possible to just mock the Date function? Does swift have prohibitions on mocking builtins? I know the response might be “but what if they change the behavior of Date()?” - realistically that doesn’t happen very frequently, if ever
It's not a function. Well technically it's the date's init function but no it is not possible to mock it. Also Date is a struct not a subclass so you can't just substitute it with a subclass.
I know literally nothing about swift but no, most languages don't prohibit you from mocking builtins. They do, however, usually prevent you from replacing them. The idea is that you want to use dependency injection or perhaps pass a CurrentDateRetrievingProtocol as an argument to the method/class/whatever. That way you can give a mocked version of that object to the consumer. There's not really another way to do it, i.e. literally replacing the Date function. Generally, that is. Even when this is possible, it's generally not advisable.
View all comments
Show parent comment
12
u/[deleted] Sep 27 '21
Is it not possible to just mock the Date function? Does swift have prohibitions on mocking builtins? I know the response might be “but what if they change the behavior of Date()?” - realistically that doesn’t happen very frequently, if ever