Пример #1
0
        public void Attack_WhenAttackIsSucessful_ShouldCallWriteline4Times()
        {
            // Arrange
            var mockedFactory = new Mock <CreaturesFactory>();
            var mockedLogger  = new Mock <ILogger>();

            var battleManager = new MockedBattleManager(mockedFactory.Object, mockedLogger.Object);

            var identifierAttacker = CreatureIdentifier.CreatureIdentifierFromString("Angel(1)");
            var identifierDefender = CreatureIdentifier.CreatureIdentifierFromString("Angel(2)");

            var creature = new Angel();

            mockedFactory.Setup(x => x.CreateCreature(It.IsAny <string>())).Returns(creature);

            mockedLogger.Setup(x => x.WriteLine(It.IsAny <string>()));

            battleManager.AddCreatures(identifierAttacker, 1);
            battleManager.AddCreatures(identifierDefender, 1);

            // Act
            battleManager.Attack(identifierAttacker, identifierDefender);

            mockedLogger.Verify(x => x.WriteLine(It.IsAny <string>()), Times.Exactly(4));
        }
Пример #2
0
        public void Attack_WhenAttackIsSucessful_ShouldCallWriteline6Times()
        {
            // Arrange
            var mockedFactory = new Mock <CreaturesFactory>();
            var mockedLogger  = new Mock <ILogger>();

            var battleManager = new MockedBattleManager(mockedFactory.Object, mockedLogger.Object);

            // The code itself should be refactored. Think about sealed class to be changed or the static method itself
            // You could use an unconstrained Mocking framework
            var identifierAttacker = CreatureIdentifier.CreatureIdentifierFromString("Angel(1)");
            var identifierDefender = CreatureIdentifier.CreatureIdentifierFromString("Angel(2)");

            var creature = new Angel();

            mockedFactory.Setup(x => x.CreateCreature(It.IsAny <string>())).Returns(creature);

            mockedLogger.Setup(x => x.WriteLine(It.IsAny <string>()));

            battleManager.AddCreatures(identifierAttacker, 1);
            battleManager.AddCreatures(identifierDefender, 1);

            // Act
            battleManager.Attack(identifierAttacker, identifierDefender);

            // Assert
            mockedLogger.Verify(x => x.WriteLine(It.IsAny <string>()), Times.Exactly(6));
        }
Пример #3
0
        public void Attack_WhenAttackingOwnArmy_ShouldThrow()
        {
            // Arrange
            var mockedFactory           = new Mock <ICreaturesFactory>();
            var mockedLogger            = new Mock <ILogger>();
            var mockedCreaturesInBattle = new Mock <ICreaturesInBattle>();

            var battleManager = new MockedBattleManager(mockedFactory.Object, mockedLogger.Object);

            // The code itself should be refactored. Think about sealed class to be changed or the static method itself
            // You could use an unconstrained Mocking framework
            var identifierAttacker = CreatureIdentifier.CreatureIdentifierFromString("Angel(1)");
            var identifierDefender = CreatureIdentifier.CreatureIdentifierFromString("Angel(1)");

            var creature = new Angel();

            mockedFactory.Setup(x => x.CreateCreature(It.IsAny <string>())).Returns(creature);
            mockedLogger.Setup(x => x.WriteLine(It.IsAny <string>()));

            battleManager.AddCreatures(identifierAttacker, 1);
            battleManager.AddCreatures(identifierDefender, 1);

            // Act and Assert
            Assert.Throws <ArgumentException>(() => battleManager.Attack(identifierAttacker, identifierDefender));
        }
        public void Attack_ShouldMakeAttackingAndDefendingCreatureCallStartNewTurnMethodExactlyOnceForEachCreature()
        {
            var mockedCreaturesFactory = new Mock <ICreaturesFactory>();
            var mockedLogger           = new Mock <ILogger>();
            var mockedAttacker         = new Mock <ICreaturesInBattle>();

            mockedAttacker.Setup(x => x.Creature).Returns(new Angel());

            var mockedDefender = new Mock <ICreaturesInBattle>();

            mockedDefender.Setup(x => x.Creature).Returns(new ArchDevil());

            var bm = new MockedBattleManager(
                mockedAttacker.Object,
                mockedDefender.Object,
                mockedCreaturesFactory.Object,
                mockedLogger.Object);

            var attackerIdentifier = CreatureIdentifier.CreatureIdentifierFromString("Angel(1)");
            var defenderIdentifier = CreatureIdentifier.CreatureIdentifierFromString("ArchDevil(2)");

            bm.Attack(attackerIdentifier, defenderIdentifier);

            mockedAttacker.Verify(x => x.StartNewTurn(), Times.Once());
            mockedDefender.Verify(x => x.StartNewTurn(), Times.Once());
        }
        public void Attack_ShouldThrowArgumentException_WhenAttackerCreatureIdentifierIsNull()
        {
            //Arrange
            var creaturesFactoryStub = new Mock <ICreaturesFactory>();
            var loggerStub           = new Mock <ILogger>();
            var sut        = new MockedBattleManager(creaturesFactoryStub.Object, loggerStub.Object);
            var identifier = CreatureIdentifier.CreatureIdentifierFromString("Angel(1)");

            //Act & Assert
            Assert.Throws <ArgumentException>(() => sut.Attack(identifier, identifier));
        }
Пример #6
0
        public void Attack_WhenAttackingCreatureIdentifierIsNull_ShouldThrowArgumentException()
        {
            // Arrange
            var mockedFactory = new Mock <ICreaturesFactory>();
            var mockedLogger  = new Mock <ILogger>();

            var battleManager = new MockedBattleManager(mockedFactory.Object, mockedLogger.Object);

            // The code itself should be refactored. Think about sealed class to be changed or the static method itself
            // You could use an unconstrained Mocking framework
            var identifier = CreatureIdentifier.CreatureIdentifierFromString("Angel(1)");

            // Act & Assert
            Assert.Throws <ArgumentException>(() => battleManager.Attack(identifier, identifier));
        }
Пример #7
0
        public void Attack_WhenAttackingOwnArmy_ShouldThrowArgumentException()
        {
            // Arrange
            var mockedFactory = new Mock <ICreaturesFactory>();
            var mockedLogger  = new Mock <ILogger>();

            var battleManager = new MockedBattleManager(mockedFactory.Object, mockedLogger.Object);

            var identifierAttacker = CreatureIdentifier.CreatureIdentifierFromString("Angel(1)");
            var identifierDefender = CreatureIdentifier.CreatureIdentifierFromString("Angel(1)");

            var creature = new Angel();

            mockedFactory.Setup(x => x.CreateCreature(It.IsAny <string>())).Returns(creature);

            mockedLogger.Setup(x => x.WriteLine(It.IsAny <string>()));

            battleManager.AddCreatures(identifierAttacker, 1);
            battleManager.AddCreatures(identifierDefender, 1);

            // Act and Assert
            Assert.Throws <ArgumentException>(() => battleManager.Attack(identifierAttacker, identifierDefender));
        }
        //[Test]
        public void Attack_WhenAttackingOwnArmy_ShouldThrow()
        {
            //Arrange
            var mockedfacttory          = new Mock <ICreaturesFactory>();
            var mockedLogger            = new Mock <ILogger>();
            var mockedCreaturesInBatlle = new Mock <ICreaturesInBattle>();

            var sut = new MockedBattleManager(mockedfacttory.Object, mockedLogger.Object);

            var identifier1 = CreatureIdentifier.CreatureIdentifierFromString("Angel(1)");
            var identifier2 = CreatureIdentifier.CreatureIdentifierFromString("Angel(1)");

            var creature = new Angel();

            mockedfacttory.Setup(x => x.CreateCreature(It.IsAny <string>())).Returns(creature);
            mockedLogger.Setup(x => x.WriteLine(It.IsAny <string>()));

            //Act
            sut.AddCreatures(identifier1, 1);
            sut.AddCreatures(identifier2, 1);

            //Assert
            Assert.Throws <ArgumentException>(() => sut.Attack(identifier1, identifier2));
        }
        public void Attack_WhenAttackIsSuccessfull_ShoulCallWriteLine6Times()
        {
            //Arrange
            var creaturesFactoryStub = new Mock <ICreaturesFactory>();
            var loggerMock           = new Mock <ILogger>();
            var sut = new MockedBattleManager(creaturesFactoryStub.Object, loggerMock.Object);

            var identifier1 = CreatureIdentifier.CreatureIdentifierFromString("Angel(1)");
            var identifier2 = CreatureIdentifier.CreatureIdentifierFromString("Angel(2)");

            var creature = new Angel();

            creaturesFactoryStub.Setup(x => x.CreateCreature(It.IsAny <string>())).Returns(creature);
            loggerMock.Setup(x => x.WriteLine(It.IsAny <string>()));


            //Act
            sut.AddCreatures(identifier1, 1);
            sut.AddCreatures(identifier2, 1);
            sut.Attack(identifier1, identifier2);

            //Assert
            loggerMock.Verify(x => x.WriteLine(It.IsAny <string>()), Times.Exactly(6));
        }