Clean Swift TDD Part 5 – Complete the VIP Cycle

In my last post, you learned how to test your presenter and format data properly for display. We also introduced a new displayFetchedOrders() method in the view controller. The purpose of this method is to gather the fetched orders in the view model and set the contents in the IBOutlets.

But we left the displayFetchedOrders() method empty because we focused on testing the presenter. We needed to make sure we were formatting the order date to the format we want and combine first and last names.

Now, let’s finish up by test driving the displayFetchedOrders() method so we can actually see the displayed orders in the UI.

We’ll follow the same TDD steps that you have been accustomed to in the last few posts:

  1. Isolate the dependencies
  2. Write the test first
  3. Draw the boundary
  4. Implement the logic

Continue reading →

Clean Swift TDD Part 4 – Presenter

In this post, you’ll complete the VIP cycle by testing the presenter for the fetch orders use case. You can read about my previous posts on testing the view controller, interactor, and worker.

The key takeaway here in this post is this. The job of the presenter is to format data for display to the user.

Our Order model’s date attribute is an NSDate and total is an NSDecimal. But the labels in the table view cell we use to display information to the user take Strings.

Your view controller and views do not need to know how the date and total are represented in the app. They just need some strings to display to the user. You’ll see how to format the order date and order total in the presenter, such that your view controller is agnostic to the data representation.

If you later decide to store the order date and total differently, you just have to modify the presenter while the view controller can remain intact. And it’ll be easy to unit test because there’s a clear input and output.

Continue reading →

Clean Swift TDD Part 3 – Worker

In part 1 and part 2, you tested the ListOrdersViewController and ListOrdersInteractor, respectively. But we left it kind of hanging because we mocked out the OrdersWorker.

In this post, you’ll test the OrdersWorker to make sure it returns the correct orders.

But first, let’s abstract away the actual details of fetching orders from a data store. You’ll develop the following data stores in the process:

  • Memory store
  • Core Data store
  • API store

The OrdersWorker can use any of these data stores. It is agnostic to the type of data store you choose to use.

Continue reading →

Clean Swift TDD Part 2 – Interactor

In my TDD part 1 post, you wrote a test to make sure ListOrdersViewController invoke the fetchOrders() method of the ListOrdersInteractor. Now, it’s time to make the interactor does something with the request. You’ll also see exactly how to delegate the business logic of fetching orders from the interactor to the worker.

Prepare the OrdersWorker for TDD

With a little foresight, we want to create an OrdersWorker to perform the following CRUD operations:

  • fetchOrders() lists the fetched orders.
  • fetchOrder() shows a selected order.
  • createOrder() creates a new order.
  • updateOrder() updates an existing order.
  • deleteOrder() deletes an order.

Also, the orders may be stored in Core Data or over the network. So the results may not be immediately available. So, let’s make these operations asynchronous and return the result in a completion block handler.

Continue reading →

Clean Swift Architecture Test Driven Development Part 1 – View Controller

I’ve written a book to teach you how to write unit tests effectively. If you want to make your tests fast so you’ll actually run them and run them often to receive immediate feedback, check out Effective Unit Testing. You’ll develop the confidence that your change will not break existing features, and never have to worry about introducing regression. Write non-fragile unit tests that are assets, not liabilities. Use TDD to write testable code that drives feature development.

In my original post on Clean Swift iOS architecture, the CleanStore example app had only one scene and very little business logic. There wasn’t another scene to segue to.

In this post, you’ll add a new ListOrders scene to list the user’s orders. The user can tap a + button to segue to the CreateOrder scene you built earlier.

You’ll learn how to drive this new feature using Test Driven Development (TDD).

Continue reading →