Is there a way to mock ArcGIS data types in Swift, such as Layer or IdentifyLayerResult? These types don’t expose public initializers, which makes direct instantiation difficult. However, for unit testing purposes, it's sometimes necessary to inject these objects.
Do you have any recommendations for how to handle this?
Thank you!
Marvin
Solved! Go to Solution.
Mocking of types isn't as easy in Swift as it is in other languages like Java or Kotlin. Really the only way is to create a protocol that represents the type you want to mock and then extend the ArcGIS type to conform to that protocol, and also add a mock type that conforms to that protocol. But then you must deal with programming to that interface (protocol) wherever you need that type. It can become quite cumbersome.
Note that this isn't a Maps SDK types issue, it's a Swift issue. The best we know how to do is to create protocols. After all, Apple doesn't provide ways to mock Foundation types, etc.
Hello and thank you for the question! Both the Maps SDK for Swift and the Swift Toolkit use DejaVu to mock network request for testing purposes.
DejaVu is open source and developed at Esri. On of the big advantages is that you can use real data for the tests. DejaVu will record the network requests and play them back on subsequent test runs, eliminating the need the go back "over the wire", which greatly reduces both the time spent running the tests and inconsistencies due to network issues.
Hi @MarkDostal, thank you very much. That was kind of what I was looking for, so I’ll definitely give it a try!
Regarding the specific case of IdentifyLayerResult: do you know if it's possible to mock this object at all? Since it doesn’t have any initializers and includes properties like geoElements and layerContents, it seems you can’t just call an API and get a JSON response to create an instance. How do you usually handle this?
Mocking of types isn't as easy in Swift as it is in other languages like Java or Kotlin. Really the only way is to create a protocol that represents the type you want to mock and then extend the ArcGIS type to conform to that protocol, and also add a mock type that conforms to that protocol. But then you must deal with programming to that interface (protocol) wherever you need that type. It can become quite cumbersome.
Note that this isn't a Maps SDK types issue, it's a Swift issue. The best we know how to do is to create protocols. After all, Apple doesn't provide ways to mock Foundation types, etc.
Yes, that's true—it's a shame! We've already tried using protocols, but as you mentioned, they quickly become cumbersome. Still, maybe at some point we could make use of Dejavu. But thanks for the information, @MarkDostal.