tags:

views:

114

answers:

2

Hi all, New to the world of TDD and I have soon find out that mocking at times is not as easy. We are using MOQ at work so I need to learn how to do this using moq

I have some code using the command pattern and works a treat.However If were to test drive it I would not know how to do it implementing the code below.

I have done the following

  • Created BaseToolStripMenuItem:ToolStripMenuItem and added a Command Property (see below)
  • Created a windows form and added a menuStrip with 2 item Open and Exit

In the form I just add to map the command to a button and all works a treat.

I would like to change the code so that I can UnitTest using Moq but cannot see how???

Can you help?

Any suggestions?

Thanks a lot!!

public interface ICommand
      {
        void Execute()
      }

       public abstract class BaseCmd :ICommand
       {
            protected ProcessMenuCommand ProcessCommand;
            protected MenuCommandFactory Factory;

            protected BaseCmd(ProcessMenuCommand processMenuCommand, MenuCommandFactory cmdfactory)
           {
              ProcessCommand = processMenuCommand;
              Factory = cmdfactory;
           }

           abstract public void Execute();
      }

    public class BaseToolStripMenuItem : ToolStripMenuItem
    {
         public BaseToolStripMenuItem()
         {
            Click += MenuItemClick;
            Command = null;
         }

         public BaseCmd Command { get; set; }

         private void MenuItemClick(object sender, EventArgs args)
         {
             if (Command != null) Command.Execute();
         }
    }
    public class MenuCommandFactory
    {
        private readonly ProcessMenuCommand _processMenuCommand;

        public MenuCommandFactory(ProcessMenuCommand processMenuCommand)
        {
            _processMenuCommand = processMenuCommand;
        }

        public OpenFileCmd OpenFile()
        {
            return  new OpenFileCmd(_processMenuCommand,this);
        }

        public ExitCmd Exit()
        {
            return new ExitCmd(_processMenuCommand, this);
        }
    }

    public class OpenFileCmd:BaseCmd
    {
        public OpenFileCmd(ProcessMenuCommand processMenu,MenuCommandFactory menuCommandFactory)
            :base(processMenu,menuCommandFactory)
        {
        }
        public override void Execute()
        {
            ProcessCommand.OpenFile();
        }
    }

    public class ProcessMenuCommand
    {
        public void OpenFile()
        {
            MessageBox.Show("Open a file");
        }

        public void Exit()
        {
            MessageBox.Show("Exiting");
        }
    }
    public class ExitCmd:BaseCmd
    {
        public ExitCmd(ProcessMenuCommand processMenu, MenuCommandFactory menuCommandFactory)
            :base(processMenu,menuCommandFactory)
        {
        }
        public override void Execute()
        {
            ProcessCommand.Exit();
        }
    }

    //In the form
    public partial class Form1 : Form
    {
        private ProcessMenuCommand menuCommandProcessor;
        private MenuCommandFactory factory;
        public Form1()
        {
            InitializeComponent();

            // Created editor and factory.
            menuCommandProcessor = new ProcessMenuCommand();
            factory = new MenuCommandFactory(menuCommandProcessor);

            // Get concrete command objects from factory and assign to corresponding menu items and tool strip buttons.
            tsOpen.Command = factory.OpenFile();
            tsExit.Command = factory.Exit();
        }
    }
A: 

However If were to test drive it I would not know how to do it implementing the code below

The idea about TDD is that it drives you towards an implementation. There are many implementations you could never arrive at using TDD, so your question doesn't really make much sense.

Try to write some tests that drive you towards your goal without having a preconceived image of the solution at which you wish to arrive. It will often turn out that you end up at an entirely different (and better) place than what you originally thought.

Mark Seemann
What you are saying it makes total sense,and it is so true.However my question would be than How could I testDrive a command pattern using Moq? Any ExamplesThanks
I am not being lazy.It;s just my knowledge of Moq and TDD that is letting me down.
I'm not saying your are lazy :) My point is that you can't let tests guide the direction if you have already decided where you want to go. At best, it's going to be much harder than it should be.
Mark Seemann
This answer describes in more details how you could approach TDD: http://stackoverflow.com/questions/1983330/design-of-mid-large-sized-application-when-doing-tdd/1983436#1983436
Mark Seemann
Thanks for the link.That post is really good and reinforce what I thought about TDD.Now,before using TDD I knew how to implement patterns etc....Now I have to write using TDD and Moq,the trouble is that at times we do get stuck and some pratical examples go a long way.I am not asking somebody to write it all for me ,but if they had already implemented it or done it before they might point me to a link or code to download etc...Thanks
A: 

A simple Novice Rule: no abstract classes. Try designing again with only interfaces and concrete classes. You'll notice it's easier to test-drive the result.

As for "how to TDD a Command object", a Command is just a class that provides a single action. Test-drive it the same way you would test-drive any method, except you name the method Execute().

J. B. Rainsberger