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)); }
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_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)); }
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)); }