Test Driven Development with MVVM
I’ve been a consultant in many projects for client side developments, and compared to the backend the test coverage is usually very low. In fact in many projects unit test coverage is zero percent. And I do understand that. Many UI technologies make it really hard to write simple unit tests. It’s very possible to deploy a product that behaves reasonably well without ever writing a unit test. Everyone who tells you something else is simply a liar. But maybe his intentions are good. Most likely he’s a consultant like me who wants to convince you that you need to write unit tests, because it will make your life so much easier.
Test Driven Development means "Test first"
If you’re able to test the behaviour of your UI in simple unit tests, you will gain a lot of confidence in the quality of your product. But not only this, you can also start using Test Driven Development practices, that will simplify your work process by giving you a clean, success oriented and structured way to easily acomplish things.
Whenever you’re creating a form, a dialog, or any other UI - don’t bother writing UI code. Instead start by writing a test for one of the aspects in your spec. Then quickly implement the tested functionality until the test - and all other tests in your project - succeed. As a last step clean up and refactor what you created. Then repeat this process with the next test. As soon as all the tests succeed you’re done. That’s TDD in a nutshell!
Is your UI Toolkit Test friendly?
The whole process works great and is a real productivity booster - if your UI technology supports it. That last “if” turns out to be a big problem with todays UI technologies. They are often mixing view and view logic in a way that cannot easily be untangled. You need to set up a lot of things for your tests to work. Let’s take JavaFX as an example. Even though there’s a separation between design and view logic ( via FXML and the accompanying controller ), this separation isn’t absolute. As a result you end up with a controller that knows and manipulates your Labels and Buttons.
This means you have to initialize those for the test. Manipulating UI Elements must happen in the UI Thread. But your unit test doesn’t run on this thread.
As a result you need to create or use a framework to solve this, and you end up with a lot of added complexity. A lot of developers - or their project managers -
don’t want to go this extra mile. That is the death of unit tests in their project.
You might suggest that it still pays to do this extra effort. I agree. But the reality shows that it’s not happening.
DukeScript to the rescue
If your UI Technology isn’t test friendly, it’s probably time to look for a better technology.
You’re reading an article about DukeScript, so it’s obvious what technology I’ll suggest now. In DukeScript we’re using a Model View ViewModel (MVVM) architecture. This has a lot of benefits for developers. Obviously it allows a clean separation of design in the “View” part and UI logic in the “ViewModel” part. It is also perfect for Test Driven Development.
You simply start by writing a unit test for the ViewModel. Then you create the ViewModel class (if it doesn’t exist yet) and implement the functionality to pass the test. Since the ViewModel knows nothing about the View, there’s no need to bother implementing the View yet. You can perfectly test your UI without it. You can test the pure functionality. As a result your test code and the ViewModel itself will be very concise.
A little example
We want to create a very simple Todo-List. The UI should have a TextField on top to enter new Items and a Button next to it to add them. The Button should only be enabled if the item entered is at least three letters long. Below these inputs we want to show the actual todo list. The user can select an item and delete it. This is done via a button below the list. It should only be enabled when an item is selected. When the button is pressed, the selected item should be removed from the list.
Let’s develop this UI using TDD. We start with iteration 1. Let’s test enabling and disabling the “add”-button first:
Now we need to write the ViewModel that passes this test:
Execute the test, it should pass. If you know what you can do with declarative bindings, you might suggest that we don’t need that ComputedProperty, because you can solve that declaratively in the view. That’s true, but then you can’t test it in a simple unit test.
That was fairly simple. We can skip the refactoring step, because the code looks OK. Ready for iteration two. Let’s write another test. This time we test if the “add”-button does it’s job:
Now lets alter the ViewModel to also pass this test:
Perfect all our tests pass. The “add”-function works. But the code is not nice. There’s a duplication for validating the input. We could check for isAddEnabled, but that would be ugly. First we might decide to add new entries via a different channel, and second the method name should reflect the purpose of the method. So let’s refactor it:
Now it’s time for the “delete”-button. We’ll first test if it’s correctly enabled and disabled:
Again lets alter the ViewModel to pass this test;
The code looks OK, no refactoring required. And finally we’ll test if pressing the delete-button really removes the item from the list:
Here are the required changes to the ViewModel:
Done, all the tests pass, our ViewModel works. Now we can start developing the View part:
All the bindings are declarative and simple. They don’t have complex binding logic. That is an important part. If we start writing complex bindings, we will often unintentionally put UI logic into the View where it is hidden from our test, and we’ll loose the benefits of TDD. But with simple bindings like in this case there’s not much that can go wrong, and we can wait with confidence for the integration tests (which are also very simple to do with DukeScript).
Obviously there is some cheating involved in this process ;-). I sometimes add the required Properties just before I write the test. That prevents unnecessary little bugs and typos like writing “getEnabled()” instead of “isEnabled()”. But generally I like to write the test as a way to design the API. It forces me to take the API user perspective. That helps to design an API that is clean and simple.
If you’re to busy to create a project and copy and paste the above code (who isn’t?), you can also checkout this project on Github
And here’s the bck2brwsr version of the example - I admit it could use some styling.
DukeScript - be productive!