adamwood2444

Coder

Jul 2nd, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1.  
  2. https://techrado.com/best-y2mate-alternatives-working/
  3.  
  4. class StubContactsDataSource: ContactsDataSource {
  5. func getAll() -> [Contact] {
  6. return [Contact(name: "Stub 1"), Contact(name: "Stub 2")]
  7. }
  8. }
  9.  
  10. // In your production code, you can use the default constructor. For example:
  11.  
  12. let productionContactsListViewController = ContactsListViewController()
  13. productionContactsListViewController.showAllContacts() // Prints Andrea and Juan. It uses the default NetworkContactsDataSource.
  14.  
  15.  
  16. // In your tests code, you can't use the default constructor, however, even though you can override the default dependency,
  17. // if you want to use StubContactsDataSource you need to override all the dependencies using by default NetworkContactsDataSource.
  18. // For example:
  19.  
  20. let presenter = ContactsListPresenter(getAllContacts: StubContactsDataSource())
  21. let testContactsListViewController = ContactsListViewController(presenter: presenter)
  22. testContactsListViewController.showAllContacts() // Prints Stub 1 and Stub 2
  23.  
  24. // As you can see, even though you want to override only the data source, you can't use the default presenter anymore.
  25. // In this example I use a very simple dependency graph, but in a real app, this NetworkDataSource could be used by a lot of other clases
  26. // You need to stop using the default constructor for all this clases to be able to replace the data source dependency. Again, all the
  27. // clases depending in those clases can't use the default constructor recursively.
  28.  
  29.  
  30. // With the proposed approach, overriding the data source for all the dependencies using it would be very easy:
  31. class TestAssembler: ContactsSceneAssembler {
  32. func resolve() -> ContactsDataSource {
  33. return StubContactsDataSource()
  34. }
  35. }
Add Comment
Please, Sign In to add comment