コード例 #1
0
 private static void InitializeApplicationCommands()
 {
     commandContainer.AddCommand("--help", new HelpCommand());
     commandContainer.AddCommand("--gen-keys", new GenKeysCommand());
     commandContainer.AddCommand("--encrypt", new EncryptCommand());
     commandContainer.AddCommand("--decrypt", new DecryptCommand());
 }
コード例 #2
0
        public void TestCommandDispatch()
        {
            var inputReaderMock = new Mock <IInputReader>();
            // Create a fake command. We want to test if it is executed.
            //
            var commandMock = new Mock <ICommand>();

            // Let accept return false always.
            //
            commandMock.Setup(x => x.Accept(
                                  It.IsAny <string>(),
                                  It.IsAny <List <string> >()))
            .Returns(false);

            var commandContainer = new CommandContainer(inputReaderMock.Object);

            commandContainer.AddCommand(commandMock.Object);

            // Define our MOCK functionality of the StartInputLoop method. If this method is called
            // our MOCK will act as a user providing a single input.
            //
            inputReaderMock
            .Setup(x => x.StartInputLoop()).Raises((x) =>
                                                   // Notice this += is a trick the Mock library uses to fire events.
                                                   x.InputEntered += null, this, new InputReadEventArgs("unknown command"));

            // Start the fake input loop, this will simulate a user input of "unknown command".
            // We expect the commandMock to not accept the input. The MOCK library will remember what happened.
            //
            inputReaderMock.Object.StartInputLoop();

            // We now check if our methods were called with the correct input. We tested what would happen if
            // the user entered "unknown command".
            // We expect that Accept() was called on the command mock once, and Execute() never.
            //

            // Set up the Execute() method of the mock, we expect it to be never called in this test.
            //
            commandMock.Verify(x =>
                               x.Execute(It.IsAny <List <string> >()), // The It class can be used to check the parameters supplied to the method.
                               Times.Never);                           // We expect the execute to not be called for the wrong command.

            // Set up the mock for the Accept() method and add some constraints we expect on the parameter.
            // This lets us test with what parameters and how often a method was called.
            //
            commandMock.Verify(x => x.Accept(
                                   // Tests the first parameter was of value "unknown"
                                   It.Is <string>((input) => input == "unknown"),
                                   // Tests the input was correctly split and the argument is a list containing one
                                   // entry with the value "command"
                                   It.Is <List <string> >((input) => input.Count == 1 && input[0] == "command")),
                               Times.Once // We expect this method to be called exactly once.
                               );
        }
コード例 #3
0
        [ExpectedException(typeof(ArgumentNullException))] // Tell the runtime to expect an exception to be thrown in this test
        public void TestAdd()
        {
            // We can not use the ConsoleInputReader to do most of the testing of the CommandContainer.
            // For one, it uses an infinite loop and also it expects user inputs. Tests should run
            // automatically without user input. We therefore create a MOCK of the interface. In this case
            // the MOCK does nothing.
            //
            var inputReaderMock  = new Mock <IInputReader>();
            var commandContainer = new CommandContainer(inputReaderMock.Object); // supply the mock object to the class

            // The test expects this call to throw an ArgumentNullException. If this call does not throw
            // this exact exception, the test will fail.
            //
            commandContainer.AddCommand(null);
        }