Exemple #1
0
        public void Start_WhenThePassedStringIsInTheFormatThatRepresentsACreateCategoryAndAddingTheNewCategory_ShouldReadParseAndExecute()
        {
            string nameOfCommand = "CreateCategory TestCategoty";
            var    categoryName  = "TestCategoty";

            StringReader sr = new StringReader(nameOfCommand);

            Console.SetIn(sr);

            var          sb = new StringBuilder();
            StringWriter sw = new StringWriter(sb);

            Console.SetOut(sw);
            var mockedFactory = new Mock <ICosmeticsFactory>();

            mockedFactory.Setup(
                x => x.CreateCategory(It.IsAny <string>()))
            .Returns(() =>
            {
                return(new Category(categoryName));
            });

            var cosmeticsEngine = new CosmeticsEngine(mockedFactory.Object, null);

            cosmeticsEngine.Start();

            Assert.AreEqual(sb.ToString().Trim(), string.Format("Category with name {0} was created!", categoryName));
        }
Exemple #2
0
        public void CallExecuteCommand_WithCorrectParameters()
        {
            // Arrange
            var commandLine   = "somecommand some parameters";
            var commandName   = commandLine.Split()[0];
            var commandParams = commandLine.Split().Skip(1).ToList();
            var dataMock      = new Mock <IDataStore>();
            var rendererMock  = new Mock <IConsoleRenderer>();
            var factoryMock   = new Mock <ICommandsFactory>();
            var commandMock   = new Mock <ICommand>();

            rendererMock.Setup(x => x.Input())
            .Returns(new List <string> {
                commandLine
            });

            factoryMock.Setup(x => x.GetCommand(It.IsAny <string>()))
            .Returns(commandMock.Object);

            // act
            var sut = new CosmeticsEngine(dataMock.Object,
                                          rendererMock.Object, factoryMock.Object);

            sut.Start();

            // assert
            commandMock.Verify(x => x.Execute(commandParams), Times.Once);
        }
        public static void Main()
        {
            var factory      = new CosmeticsFactory();
            var shoppingCart = new ShoppingCart();
            var engine       = new CosmeticsEngine(factory, shoppingCart);

            engine.Start();
        }
        public static void Main()
        {
            var factory              = new CosmeticsFactory();
            var shoppingCart         = new ShoppingCart();
            var consoleCommandParser = new ConsoleCommandParser();
            var engine = new CosmeticsEngine(factory, shoppingCart, consoleCommandParser);

            engine.Start();
        }
Exemple #5
0
        public void Start_WhenTheInputStringIsNotInTheCorrectFormat_ShouldThrowArgumentNullException()
        {
            var          cosmeticsEngine = new CosmeticsEngine(null, null);
            string       nameOfCommand   = " FirstParam";
            StringReader sr = new StringReader(nameOfCommand);

            Console.SetIn(sr);

            Assert.Throws <ArgumentNullException>(() => cosmeticsEngine.Start());
        }
        public void TestStart_PassInvalidCommand_ShouldThrowArgumentNull(string commandText)
        {
            var factory         = new Mock <CosmeticsFactory>();
            var cart            = new Mock <ShoppingCart>();
            var cosmeticsEngine = new CosmeticsEngine(factory.Object, cart.Object);

            Console.SetIn(new StringReader(commandText));

            Assert.Throws <ArgumentNullException>(() => cosmeticsEngine.Start());
        }
        public void Start_ShouldThrow_WhenInputStringCommandIsNotInTheCorrectFormat()
        {
            Console.SetIn(new StringReader("name \r\n\r\n"));
            var mockFactory = new Mock <ICosmeticsFactory>();
            var mockCart    = new Mock <IShoppingCart>();

            var engine = new CosmeticsEngine(mockFactory.Object, mockCart.Object);

            Assert.Throws <ArgumentNullException>(() => engine.Start());
        }
        public void TestStart_CreateCategory_ShouldCallCreateCategoryFromFactory()
        {
            var factory         = new Mock <ICosmeticsFactory>();
            var cart            = new Mock <IShoppingCart>();
            var cosmeticsEngine = new CosmeticsEngine(factory.Object, cart.Object);

            Console.SetIn(new StringReader("CreateCategory Category"));
            factory.Setup(x => x.CreateCategory(It.IsAny <string>())).Returns(It.IsAny <Category>());
            cosmeticsEngine.Start();

            factory.Verify(x => x.CreateCategory(It.IsAny <string>()), Times.Once);
        }
        public void Start_ShouldCreateANewCategory_WhenInputIsInAValidFormat()
        {
            Console.SetIn(new StringReader("CreateCategory ForMale\r\n\r\n"));
            Console.SetOut(new StringWriter());

            var mockFactory = new Mock <ICosmeticsFactory>();
            var mockCart    = new Mock <IShoppingCart>();
            var engine      = new CosmeticsEngine(mockFactory.Object, mockCart.Object);

            mockFactory.Setup(factory => factory.CreateCategory(It.IsAny <string>())).Returns(new Mock <ICategory>().Object);

            engine.Start();

            mockFactory.Verify(mock => mock.CreateCategory(It.IsAny <string>()), Times.Once());
        }
        public void Start_ShouldThrowArgumentNullExceptionWhenTheInputStringIsNotInTheCorrectFormat()
        {
            Console.SetIn(new StringReader("CreateToothpaste     "));

            var factory      = new Mock <ICosmeticsFactory>();
            var shoppingCart = new Mock <IShoppingCart>();
            //factory.Setup(x => x.CreateCategory(It.IsAny<string>())).Returns(new Category("ForMale"));

            var engine = new CosmeticsEngine(factory.Object, shoppingCart.Object);

            Assert.Throws <ArgumentNullException>(() => engine.Start());

            //engine.Start();

            //factory.Verify(x => x.CreateCategory(It.IsAny<string>()), Times.Once());
        }
Exemple #11
0
        public void CallCreateCommandFactory_WithCorrectCommandParameters()
        {
            // Arrange
            var dataMock     = new Mock <IDataStore>();
            var rendererMock = new Mock <IConsoleRenderer>();
            var factoryMock  = new Mock <ICommandsFactory>();

            rendererMock.Setup(x => x.Input())
            .Returns(new List <string> {
                "somecommand some parameters"
            });
            var commandNameStub = "somecommand";

            // Act
            var sut = new CosmeticsEngine(dataMock.Object,
                                          rendererMock.Object, factoryMock.Object);

            sut.Start();
            // Assert
            factoryMock.Verify(x => x.GetCommand(commandNameStub), Times.Once);
        }
Exemple #12
0
        public void ReturnCorrectResultFromExecuteCommand_WithCorrectCommandParameters()
        {
            // Arrange
            var commandLine     = "somecommand some parameters";
            var expectedMessage = "Success!";
            var commandName     = commandLine.Split()[0];
            var commandParams   = commandLine.Split().Skip(1).ToList();
            var dataMock        = new Mock <IDataStore>();
            var rendererMock    = new Mock <IConsoleRenderer>();
            var factoryMock     = new Mock <ICommandsFactory>();
            var commandMock     = new Mock <ICommand>();

            rendererMock.Setup(x => x.Input())
            .Returns(new List <string> {
                commandLine
            });

            factoryMock.Setup(x => x.GetCommand(It.IsAny <string>()))
            .Returns(commandMock.Object);

            commandMock.Setup(x => x.Execute(commandParams))
            .Returns(expectedMessage);

            // act
            var sut = new CosmeticsEngine(dataMock.Object,
                                          rendererMock.Object, factoryMock.Object);

            sut.Start();
            var actualMessage = commandMock.Object.Execute(commandParams);

            // Assert
            var outputList = new List <string>()
            {
                expectedMessage
            };


            rendererMock.Verify(x => x.Output(outputList), Times.Once);
        }