开发者

Moq and Command Pattern .I am struggling can you help?

开发者 https://www.devze.com 2022-12-16 11:34 出处:网络
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

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;
       开发者_Go百科     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();
        }
    }


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.


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().

0

精彩评论

暂无评论...
验证码 换一张
取 消