Re: [TFUI] Dialogs and EventHandlers

Tim Haughton <[email protected]> Mon, 7 Nov 2005 13:30:59 +0000
Newsgroups gmane.comp.programming.test-first-user-interfaces
Message-ID <[email protected]>
On 07/11/05, Anthony Williams <anthony_w.geo-/[email protected]> wrote:

Hi Anthony,

> http://www.livejournal.com/users/anthony_w/8000.html
>
> The key difference is that I believe the GetFontFromUserSelection function
> does not belong on the MyForm class, so I would extract it to a collaborator
> instead of mocking the FontDialog.

If I may be so bold, I think yourself and Cory have missed a trick. I
might be biased here, since I have an innate aversion to declaring
anything 'too simple to break', but essentially, I'd have:

MyForm : Form
{
     MyForm( MyCollaborator mc)
     {
          this.mc= mc;
          this.mc.Parent = this;
     }
}

The model class would look something like...

MyCollaborator
{
     public virtual Font GetFontFromUserSelection( Font defaultFont )
     {
          FontDialog fd = new FontDialog();
          _GetFontFromUserSelection( fd, defaultFont );
     }

     public virtual DialogueResult _GetFontFromUserSelection(
          FontDialogue fd, Font defaultFont )
     {
          fd.Font = defaultFont;
          if(fd.ShowDialog( Parent ) != DialogResult.Cancel )
          {
               return Fdialog.Font;
          }

          return defaultFont;
     }
}

The event handler in the form would look like....

private void menuItem15_Click(object sender, System.EventArgs e)
{
  rtbDisplay.Font = new mc.GetFontFromUserSelection(rtbDisplay.Font);
}


So, now we have a few interesting options for testing (which of course
we wrote first :) ). We can mock out the collaborator (don't like the
term, just being consistent with thread) to check that event handlers
are calling the right methods on the collaborator.

We can test _GetFontFromUserSelection by passing in mocks for the
FontDialogue and the Font (using the dynamic mock I posted earlier
today?) to make sure that's working OK. We can also test that
GetFontFromUserSelection calls _GetFontFromUserSelection with the
correct arguments. So we have 100% test coverage for our little
adventure.

The MVC approach (a la Humble Dialogue Box) opens up possibilities for
excellent test coverage at the UI. The fact that .Net blurs the View
and Controller shouldn't really matter.

Cheers,

Tim Haughton


------------------------ Yahoo! Groups Sponsor --------------------~--> 
Fair play? Video games influencing politics. Click and talk back!
http://us.click.yahoo.com/T8sf5C/tzNLAA/TtwFAA/nhFolB/TM
--------------------------------------------------------------------~-> 

To unsubscribe, email:
TestFirstUserInterfaces-unsubscribe-hHKSG33TihhbjbujkaE4pw@public.gmane.org
 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/TestFirstUserInterfaces/

<*> To unsubscribe from this group, send an email to:
    TestFirstUserInterfaces-unsubscribe-hHKSG33TihhbjbujkaE4pw@public.gmane.org

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/