Event Hookup Part Two

So what did I learn, that by creating a class that inherits EventArgs i can pass any information I need in an event, I did write a quick presenter and its test to ensure that the ITask interface worked as I expected, i.e. in exactly the same way as any other EventHandler interface definition that I have done.

The presenter Tests

   1: using Microsoft.VisualStudio.TestTools.UnitTesting;
   2: using Rhino.Mocks;
   3:  
   4: namespace ExamplePresenter.Tests
   5: {
   6:     [TestClass]
   7:     public class PresenterTests
   8:     {
   9:         private static MockRepository _mockery;
  10:         private ITask _mockTask;
  11:         private IView _mockView;
  12:         public TestContext TestContext { get; set; }
  13:  
  14:         [TestInitialize]
  15:         public void TestInitialize()
  16:         {
  17:             _mockery = new MockRepository();
  18:  
  19:             _mockView = _mockery.StrictMock<IView>();
  20:             _mockTask = _mockery.StrictMock<ITask>();
  21:         }
  22:  
  23:         [TestMethod]
  24:         public void Should_Hook_Up_Events_On_Creation()
  25:         {
  26:             using (_mockery.Record())
  27:             {
  28:                 _mockView.ButtonPressed += null;
  29:                 LastCall.IgnoreArguments();
  30:  
  31:                 _mockTask.ProgressMessage += null;
  32:                 LastCall.IgnoreArguments();
  33:             }
  34:  
  35:             using (_mockery.Playback())
  36:                 CreateSUT();
  37:         }
  38:  
  39:         private Presenter CreateSUT()
  40:         {
  41:             return new Presenter(_mockView, _mockTask);
  42:         }
  43:     }
  44: }

and the presenter code, all fairly standard

   1: using System;
   2:  
   3: namespace ExamplePresenter
   4: {
   5:     public class Presenter
   6:     {
   7:         private readonly ITask _task;
   8:         private readonly IView _view;
   9:  
  10:         public Presenter(IView view, ITask task)
  11:         {
  12:             _view = view;
  13:             _task = task;
  14:  
  15:             HookupEvents();
  16:         }
  17:  
  18:         private void HookupEvents()
  19:         {
  20:             _view.ButtonPressed += ButtonPressedEvent;
  21:             _task.ProgressMessage += ProgressMessageEvent;
  22:         }
  23:  
  24:         private void ButtonPressedEvent(object sender, EventArgs e)
  25:         {
  26:             throw new NotImplementedException();
  27:         }
  28:  
  29:         private void ProgressMessageEvent(object sender, MessageEventArgs args)
  30:         {
  31:             throw new NotImplementedException();
  32:         }
  33:     }
  34: }
  35:  
  36: using System;
  37:  
  38: namespace ExamplePresenter
  39: {
  40:     public interface IView
  41:     {
  42:         event EventHandler ButtonPressed;
  43:     }
  44: }

So why have I put all this here, just so I remember 🙂

Have Fun.

Advertisements

About Duncan Butler

Trying to be a very agile software developer, working in C# with Specflow, Nunit and Machine Specifications, and in the evening having fun with Ruby and Rails
This entry was posted in Programming. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s