public void SetupRandomBattle_OnlyOneHero_ShouldThrowInvalidOperationException() { var battleServiceBuilder = new BattleServiceBuilder().WithHeroes(1); IBattleService service = battleServiceBuilder.Build(); Assert.That(() => service.SetupRandomBattle(), Throws.InvalidOperationException); }
private static (int, int) AssertRandomBattleSetup(int amountOfHeroes) { //Arrange int index1 = -1, index2 = -1; var battleServiceBuilder = new BattleServiceBuilder().WithHeroes(amountOfHeroes); IBattle createdBattle = null; battleServiceBuilder.BattleFactoryMock .Setup(factory => factory.CreateNewBattle(It.IsAny <IHero>(), It.IsAny <IHero>())) .Callback((IHero fighter1, IHero fighter2) => { index1 = battleServiceBuilder.AllHeroes.ToList().IndexOf(fighter1); index2 = battleServiceBuilder.AllHeroes.ToList().IndexOf(fighter2); Assert.That(index1, Is.GreaterThanOrEqualTo(0), "You must use a hero from the repository to create the battle using the factory."); Assert.That(index2, Is.GreaterThanOrEqualTo(0), "You must use a hero from the repository to create the battle using the factory."); Assert.That(index1, Is.Not.EqualTo(index2), "The same hero cannot be picked twice in one battle."); createdBattle = new Mock <IBattle>().Object; }) .Returns((IHero fighter1, IHero fighter2) => createdBattle); IBattleService service = battleServiceBuilder.Build(); //Act IBattle battle = service.SetupRandomBattle(); //Assert Assert.That(battle, Is.Not.Null, "The returned battle is null."); battleServiceBuilder.HeroRepositoryMock.Verify(repo => repo.GetAll(), Times.Once, "The repository should be used to retrieve all heroes."); battleServiceBuilder.BattleFactoryMock.Verify( factory => factory.CreateNewBattle(It.IsAny <IHero>(), It.IsAny <IHero>()), Times.Once, "The CreateNewBattle method of the factory has not been called."); Assert.That(battle, Is.SameAs(createdBattle), "The battle created by the factory is not the instance that is returned."); return(index1, index2); }