예제 #1
0
        public void Serialize_Exception()
        {
            var target = new UnknownCommandException();

            target = new UnknownCommandException("message", new Exception());
            target = new UnknownCommandException("Command1", "message", new Exception());

            var @string = target.ToString();

            var bf = new BinaryFormatter();

            using (var ms = new MemoryStream())
            {
                // "Save" object state
                bf.Serialize(ms, target);

                // Re-use the same stream for de-serialization
                ms.Seek(0, 0);

                // Replace the original exception with de-serialized one
                target = (UnknownCommandException)bf.Deserialize(ms);
            }

            // Double-check that the exception message and stack trace (owned by the base Exception) are preserved
            target.ToString().Should().Be(@string);
        }
예제 #2
0
        public void Execute_UnknownCommand()
        {
            TextWriter outputStream = new StringWriter();
            TextWriter errorStream  = new StringWriter();

            CommandExecutor executor = new CommandExecutor("TestContext", outputStream, errorStream);

            executor.AddCommand <OutputCommand>();

            UnknownCommandException exception = TestUtils.TestForError <UnknownCommandException>(
                () => executor.Execute("Error hello"),
                "Did not catch expected exception");

            Validate.Value.AreEqual("Error", exception.CommandName,
                                    "Error did not occur with the correct command.");
        }
        public void Execute_WithUnknownCommand_LogUnknownCommandException()
        {
            // Given: An exception of Invalid Command Data
            const string cmd       = "unknown";
            var          exception = new UnknownCommandException(cmd);

            InputMock.Setup(input => input.ReadLine()).Returns($"/{cmd}");
            CommandStorageMock.Setup(parser => parser.FetchCommandFromName(cmd))
            .Throws(exception);

            // When: Execute is called
            CommandExecutor.Execute();

            // Verify: Execption thrown is being logged
            LoggerMock.Verify(logger => logger.LogWarning(exception.Message, ""));
        }