Esempio n. 1
0
        public void StopsExecutionAfterRobotWasLost()
        {
            _commandFactoryMock.Setup(x => x.IsSupported(It.IsAny <char>())).Returns(true);

            var aMock = new Mock <ICommand>();

            aMock.Setup(x => x.ExecuteAsync(It.IsAny <IGrid>(), It.IsAny <IRobot>())).Returns(Task.FromResult(0));
            _commandFactoryMock.Setup(x => x.GetCommand(It.Is <char>(c => c == 'a'))).Returns(aMock.Object);

            var bMock = new Mock <ICommand>();

            bMock.Setup(x => x.ExecuteAsync(It.IsAny <IGrid>(), It.IsAny <IRobot>()))
            .Callback((IGrid grid, IRobot robot) => robot.Status = RobotStatus.LOST)
            .Returns(Task.FromResult(0));
            _commandFactoryMock.Setup(x => x.GetCommand(It.Is <char>(c => c == 'b'))).Returns(bMock.Object);

            var cMock = new Mock <ICommand>();

            cMock.Setup(x => x.ExecuteAsync(It.IsAny <IGrid>(), It.IsAny <IRobot>())).Returns(Task.FromResult(0));
            _commandFactoryMock.Setup(x => x.GetCommand(It.Is <char>(c => c == 'c'))).Returns(cMock.Object);

            var command = new TextCommand("abc", _commandFactoryMock.Object);

            command.ExecuteAsync(_gridMock.Object, _robot).Wait();

            aMock.Verify(x => x.ExecuteAsync(It.IsAny <IGrid>(), It.IsAny <IRobot>()), Times.Once);
            bMock.Verify(x => x.ExecuteAsync(It.IsAny <IGrid>(), It.IsAny <IRobot>()), Times.Once);
            cMock.Verify(x => x.ExecuteAsync(It.IsAny <IGrid>(), It.IsAny <IRobot>()), Times.Never);
        }
Esempio n. 2
0
        public void ExecutesAllCommandsOneByOne()
        {
            string actualString = "";

            _commandFactoryMock.Setup(x => x.IsSupported(It.IsAny <char>())).Returns(true);

            var aMock = new Mock <ICommand>();

            aMock.Setup(x => x.ExecuteAsync(It.IsAny <IGrid>(), It.IsAny <IRobot>())).Callback(() => actualString += "a");
            _commandFactoryMock.Setup(x => x.GetCommand(It.Is <char>(c => c == 'a'))).Returns(aMock.Object);

            var bMock = new Mock <ICommand>();

            bMock.Setup(x => x.ExecuteAsync(It.IsAny <IGrid>(), It.IsAny <IRobot>())).Callback(() => actualString += "b");
            _commandFactoryMock.Setup(x => x.GetCommand(It.Is <char>(c => c == 'b'))).Returns(bMock.Object);

            var cMock = new Mock <ICommand>();

            cMock.Setup(x => x.ExecuteAsync(It.IsAny <IGrid>(), It.IsAny <IRobot>())).Callback(() => actualString += "c");
            _commandFactoryMock.Setup(x => x.GetCommand(It.Is <char>(c => c == 'c'))).Returns(cMock.Object);

            var command = new TextCommand("abc", _commandFactoryMock.Object);

            command.ExecuteAsync(_gridMock.Object, _robot).Wait();

            Assert.AreEqual("abc", actualString);
        }