Re: Testing Views - iOS

"Matteo Vaccari [email protected] [testdrivendevelopment]" <[email protected]> Sun, 1 Jun 2014 10:49:22 +0200
Newsgroups gmane.comp.programming.test-driven-development
Message-ID <CADwhwbO6YomwL_--WCx8ttXZuTbjf9U2Zzqt7zU80A-aEnKdPg@mail.gmail.com>
On Fri, May 30, 2014 at 4:22 PM, Luther Baker [email protected]
[testdrivendevelopment] <[email protected]> wrote:

>
>
> I've essentially broken my GUI logic out into a rough MVP pattern and
> while I find testing the Models and Presenters straightforward, I haven't
> come up with a repeatable process to test drive the code in the Views.
>
> Granted, my views are dumb and have no real business logic - but for
> instance, if something is disabled, I could test that certain fields are
> set to 'readonly', or greyed out, etc.
>
> I generally use mocks and inject dependencies everywhere else in the app -
> but that isn't as feasible in the views. Buttons, toggles and segemented
> controls aren't something the parent view can just swap out, replace and
> layout correctly. Injection doesn't feel right and so, because Views are so
> 'self contained' - I find myself newing child components up inside of their
> parent views and as such, I'm not sure how to TDD them without exposing
> their internals somehow.
>
> Would great appreciate some suggestions.
>
> Thanks,
> -Luther
>
>
I don't know anything about iOS programming, so what I say might be
completely useless to you.  Anyway I will try a suggestion:

Can you imagine to test drive not the view themselves, but the code that
builds the view?  If this was a web application, I may want to write
something like

the view builder:
...
view.addHeading("foo");
view.addParagraph("bar");
...

the view object contains methods that make calls to the display technology.
 In html that would be just

void addHeading(String content) {
  this.html += "<h1>" + content + "</h1>";
}

in iOS you would have calls to the iOS APIs in there.  The view builders
would know nothing of iOS and could be easily test-driven.

Once you have a

void addGrayedOutButton(String title)

method working, and you know that it works because you've seen it working
once, you shouldn't need much more testing unless you change its body.

Matteo