¿ªÔÆÌåÓý

ctrl + shift + ? for shortcuts
© 2025 Groups.io

[TDD] Testing Views - iOS


 




On Fri, May 30, 2014 at 4:22 PM, Luther Baker lutherbaker@... [testdrivendevelopment] <testdrivendevelopment@...> 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 += "

" + content + "

";
}

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





?


 

¿ªÔÆÌåÓý

If your views are dumb then you are doing it right. This means the only thing you views will define is the mapping between the UI events of the view and the logical events of the presenter and vice-versa. This means that the only way to test the View is by looking at the actual UI and making sure that the mapping is done correctly. This means that for the most part the View part is just an adapter ¨C it takes a button click, or a menu choice, or a gesture, or a text (CLI), or voice instruction{IVR}, or a dial tone (DTMF), a function call (API), or an HTTP command (another type of API), or an email message (yet another type of UI), and converts them into a user event ¨C e.g., SEARCH(). In the other direction ¨C it takes a user interaction ¨C e.g., Show Search Result(), and turns it into the¡ªsetting of text into a text field, or a list into a list box, or a generated voice, or an email, or a function response, or an email response, or ah HTTP reply, or whatever, depending on the type of interface your are implementing.

?

This means that the View is extremely simple, and that the Presenter (or Controller) has no idea what UI is. In fact, it defines its own ABSTRACT, domain specific, UI which you mock to test the Presenter and implement for every UI.

?

Where this becomes interesting is under two circumstances:

1.?????? The UI has specific UI interaction that need to be tested ¨C i.e., when you¡¯re implementing specific UI widgets ¨C e.g., a List box (Scrollable), or Drag and drop, in which case you¡¯ll find that the View can be further separated into its own MVP, but that this time the domain is not the user¡¯s domain but rather the UI domain. This will allow you to define abstract operations like ¡®Show Scroll Bar¡¯ or ¡®Mouse Click¡¯, which you can mock.

2.?????? Internationalization ¨C And other Skin related attributes like colors, fonts, etc. These are obviously UI specific, hence not meaningful to the Presenter, but should still be testable. If you want these to be fixed, you can just test them by inspection (automated of manual). If however you want to them to change (as in i18n or locale information) then these values come from the Model in the UI MVP.

?

?

So -?

[ Model ] == [ Presenter ] == [ View]??

is the application view, where the only difficult thing to test is the view (need to test with real UI).

But, if you have an specific UI interactions it will really look like ¨C

[ Model ] == [ Presenter ] == [[ uiModel ] == [ uiPresenter ] == [ uiView ]]

?

Which gives you two new layers of mocking and easy testing, if you need them.

From: testdrivendevelopment@... [mailto:testdrivendevelopment@...]
Sent: Sunday, June 1, 2014 12:49 PM
To: testdrivendevelopment@...
Subject: Re: [TDD] Testing Views - iOS

?

?

?

?

On Fri, May 30, 2014 at 4:22 PM, Luther Baker lutherbaker@... [testdrivendevelopment] <testdrivendevelopment@...> 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 += "

" + content + "

";

}

?

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

?

?

?

?

?

?