Forty seconds to test one comparison
A timesheet app has one test for overtime, and it takes 40 seconds. It starts a browser, signs in, types five numbers into five fields and reads the colour of a label, and now and then it fails on timing. The rule it checks is one comparison: more than 40 hours in a week is overtime, and the weekly total turns red.
It needs a browser because the comparison lives in an input handler. The handler reads the five fields from the page, adds them, compares the sum with 40, paints the label, and disables the Submit button when any day passes 24 hours. To run the comparison, you have to run the page.
Three jobs in one handler
That handler mixes three things that change for different reasons. The domain rules: a day holds at most 24 hours, and hours above 40 a week are overtime; payroll owns those. The presentation logic: show the total as "43 h", turn it red when there is overtime, enable Submit only when the week is valid; whoever designs the screen owns those. And the drawing itself, which belongs to the UI toolkit. Pulled apart, the comparison can be tested in milliseconds, the mobile app can reuse the rules, and a redesign stops touching payroll's code.
The patterns of the MV* family all start by pulling them apart the same way. A Model owns the domain state and rules: the hours per day, the weekly total, the overtime. It knows nothing about screens, so the web form and the mobile app can sit on the same Model. A View draws what the user sees and receives what they type, and does not compute overtime. So the domain rules go to the Model and the drawing to the View. The presentation logic is what the members of the family place differently: MVC usually leaves it in the View, while MVP and MVVM move it into a piece between Model and View.
One question, three answers
Ask each pattern the same thing about the red total: who decides that 43 hours means a red label, and how does that decision reach the screen?
- MVC sends the user's input to a Controller, which calls the Model; the Model announces that it changed, and each View re-reads it and redraws. The decision usually stays in the View, so unless it is split out of the drawing code, testing it means rendering one. MVC: Model, View, Controller covers the loop, and the different thing the name means in web frameworks.
- MVP moves the decision into a Presenter, which talks to the View through a small interface and pushes each result to it by hand, such as
setOvertimeWarning(true). A test gives the Presenter a fake View and checks the call. MVP: Model, View, Presenter covers the interface, its variants and what the hand-wiring costs. - MVVM puts the decision in a ViewModel, which exposes view-ready state, such as a
showOvertimeWarningflag the label's colour is bound to, and never references the View; a binding layer carries changes between the two. MVVM: Model, View, ViewModel covers binding and where it bites.
For the timesheet, all three move the overtime rule into the Model, where its test runs in milliseconds. MVP and MVVM, by design, also take the decision to warn out of the drawing code, which only picks the red, and each charges for it: MVP in calls written by hand for every value the screen shows, MVVM in a binding layer whose behaviour no longer appears in your own code.
Picking one, and keeping to it
Start from the toolkit. One with declarative data binding, as WPF and several web frameworks offer, makes MVVM cheap; without binding, MVP's hand-written calls are the usual price of presentation logic a test can reach; and a screen whose presentation logic is a couple of formatting calls gets little back from either, so a Model kept free of widgets may be all it needs. Then keep to the one you chose, across the app or at least within each module: a codebase that mixes all three screen by screen pays for all three, with a different kind of test per screen and a different place to look for each decision.
How the split leaks
None of these patterns enforces itself. The leak that undoes the most is the handler the timesheet started with: business logic in the View "just this once", say the 24-hour check added to Tuesday's change handler because a release was close. The rule now exists twice, in the Model and in the View, every change to that screen can break a payroll rule, and when payroll changes the limit, the copy nobody remembers keeps the old one.
The leak in the other direction is quieter. A Model that formats "43 h" or knows the warning colour drags presentation into payroll's rules, and the mobile app, which shows overtime as a badge, has to work around a string and a colour meant for another screen. The middle piece leaks too, when a Presenter or ViewModel gets hold of a concrete widget; MVVM: Model, View, ViewModel shows how that happens and how to undo it.
The split, on a Grid
The Model-View-Controller Challenge is the plainest test of the split: a Model holding the data and rules, a View rendering it, a Controller taking the user's input and updating the Model, and the flow from the user through all three. Draw it for the timesheet, then mark which node calculates the overtime, which formats "43 h" and which picks the red. Overtime in the Model and the other two in the View is MVC working as designed; overtime in the View, or formatting or colour in the Model, means the split has leaked, and the mobile app will pay for it.