MVVM and Tests

In last weeks , I wrote about MVVM with Flow Controller. I created an example in my github and explain my idea about software architecture in Apps. I don’t use TDD in the first moment , I created a first version of the class and after I create a simple test.At first I did small unit tests, such as chamadando the results of the ViewModel. Quickly I ended up covering 50% of the project with tests.

But first I want to talk about architecture versus testing.

In MVVM it is easier to test?

I don't believe that Unit Test haven't associate with App Architecture . So the answer is : NO, but the "Loose coupling" is a great point in this case.

Unit Test

This is a very simple case of Unit Test, I create a ListViewModel with our Model. This model has a fixed number of Owl ( 21 units) . Very simple!

That’s the advantage of reusing the viewmodel codes using Generic, with a test I will cover all of my lists. This causes few tests guarantee the operation of our project. I did both tests for the positives and the negatives, I adopted a policy of simply return Nil if something went wrong and deal with it in the interface.

To understand a little better the use of Generics gains for reuse code you can do with Chris Eidhof if you do not know it please stop reading and read what he writes that it is much cooler. :)


Introduced in XCode 7, UITests now are simple to develop. I’d rather create the test without help tool to record the tests. That’s because I like to really understand the step-by-step I’m modeling for testing. Below is a simple example of detail screen test.

But you must be wondering how the test only tests the details of an item without making the whole process. The idea is that we do not always realize all streams to test an interface, then taking advantage of the Flow Controller created a way to track which stream the test will use.

In the next chapter I explain my strategy.

Routes to Tests

In my previous text MVVM with Flow Controller, I explain about Flow Controller . One problem I’ve always had in the projects that tried to implement UI testing is the case of very large flows with very complex screens, can take too long to run.

But in my view there are two types of UI tests, which test the screens and the interface and the full flow testing an app (for m-commerce, a full-purchase). But for use during the development process to revalidate all possible flows it is not interesting and very time consuming.

So taking advantage of the Flow system, I can create UI tests for screens or small sets of intertaces. Now that’s interesting test how much these will be shared code is important to validate that fits on that basis did not affect other parts of the app.

Passing argument as a test flag and which route I want to simulate, I can create different minor flow to make my tests. It is a simple solution and a first version.


I do not believe that increased testing is related to architecture, but the percentage of coverage is easier to achieve. If your concern is quickly cover the largest percentage I think the MVVM will help a lot. Mostly reusing the basic structures with Generic.

In cases of functional tests I liked simulate the improvement of power simpler routes to perform several short tests. At the end also I was able to test different solutions on the same device such as Grid or TableView. This gives an interesting result, and increases test coverage results.

At the end of a 96% coverage aproximadamento 1 hour work. This is very good, even on a small project like this.

Next Steps

Next steps in this MVVM design:

  • Expand the project with more models and more interfaces;
  • Making ViewController layer more Generic and reusable;
  • Create your own networking layer simply.

These items may take a while because I will have to climb a Webservice to facilitate the implementation of new ideas. (2 weeks)


My wife , she make a coffee to me . Yes this very important .

I would like the share in last article, in special to : 
- Steve “Scotty” Scott
Krzysztof Zabłocki

Accompany these guys, they talk a lot of interesting things.

This week I made a Maratone and saw all the videos ObjcIO, really one of the most interesting independent sites that exist for Swift and Objective-C developers. In particular, Chris Eidhof that inspired me to try to create my interfaces programmatically because it could create extremely interesting things. Seriously accompany them.

Any suggestion is welcome. Anything come to me on Twitter to discuss. Thank you!

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.