Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- https://techrado.com/best-y2mate-alternatives-working/
- class StubContactsDataSource: ContactsDataSource {
- func getAll() -> [Contact] {
- return [Contact(name: "Stub 1"), Contact(name: "Stub 2")]
- }
- }
- // In your production code, you can use the default constructor. For example:
- let productionContactsListViewController = ContactsListViewController()
- productionContactsListViewController.showAllContacts() // Prints Andrea and Juan. It uses the default NetworkContactsDataSource.
- // In your tests code, you can't use the default constructor, however, even though you can override the default dependency,
- // if you want to use StubContactsDataSource you need to override all the dependencies using by default NetworkContactsDataSource.
- // For example:
- let presenter = ContactsListPresenter(getAllContacts: StubContactsDataSource())
- let testContactsListViewController = ContactsListViewController(presenter: presenter)
- testContactsListViewController.showAllContacts() // Prints Stub 1 and Stub 2
- // As you can see, even though you want to override only the data source, you can't use the default presenter anymore.
- // 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
- // You need to stop using the default constructor for all this clases to be able to replace the data source dependency. Again, all the
- // clases depending in those clases can't use the default constructor recursively.
- // With the proposed approach, overriding the data source for all the dependencies using it would be very easy:
- class TestAssembler: ContactsSceneAssembler {
- func resolve() -> ContactsDataSource {
- return StubContactsDataSource()
- }
- }
Add Comment
Please, Sign In to add comment