public void Setup() { _serviceProviderBuilder = FakeServiceProviderBuilderForJob.DefaultBuilder() .AddBuildingRepository() .AddUnitRepository(); _serviceProviderBuilder.WithService(BuildingPlanProviderFake.GetBuildingRepository()); }
public void Test_Prepare_To_Attack() { var actionProviderMock = new Mock <IActionProvider>(); actionProviderMock.Setup(x => x.GetActionsForPlayer(It.IsNotNull <TravianUser>())) .Returns(Task.FromResult((IEnumerable <GameAction>)Builder <GameAction> .CreateListOfSize(2) .TheFirst(1) .With(x => x.Action = GameActionType.TRAIN_ARMY) .With(x => x.Village = Builder <Village> .CreateNew().With(y => y.Name = "test_village_1").Build()) .TheNext(1) .With(x => x.Action = GameActionType.SEND_RESOURCES) .With(x => x.Village = Builder <Village> .CreateNew().With(y => y.Name = "test_village_2").Build()) .Build())); var gameplayMock = new Mock <IGameplayClient>(); gameplayMock .Setup(x => x.ExecuteActions(It.IsNotNull <TravianUser>(), It.IsNotNull <List <GameAction> >())) .Returns(Task.FromResult(Builder <BaseScenarioResult> .CreateNew().Build())); _serviceBuilder = _serviceBuilder .WithService(actionProviderMock.Object) .WithService(gameplayMock.Object); var job = new PrepareToAttackJob(_serviceBuilder.Build()); //var context = new Mock<IJobExecutionContext>(); //var jobDetail = new Mock<IJobDetail>(); var data = new JobExecutionData { TravianUser = FakeDataProvider.GetUser(PlayerStatus.UNDER_ATTACK, true), JobType = typeof(PrepareToAttackJob) }; //jobDetail.Setup(x => x.JobDataMap[AbstractJob.JobExecutionDataKey]).Returns(data); //context.Setup(x => x.JobDetail).Returns(jobDetail.Object); Assert.DoesNotThrowAsync(async() => await job.Execute(data)); _serviceBuilder.LoggerMock.Verify(x => x.Log( LogLevel.Error, It.IsAny <EventId>(), It.IsAny <It.IsAnyType>(), It.IsAny <Exception>(), (Func <It.IsAnyType, Exception, string>)It.IsAny <object>()), Times.Never); Assert.AreEqual(2, _serviceBuilder.Messages.Count); Assert.AreEqual($"The village test_village_1 was prepared to attack with following action: [TRAIN_ARMY].", _serviceBuilder.Messages[0]); Assert.AreEqual($"The village test_village_2 was prepared to attack with following action: [SEND_RESOURCES].", _serviceBuilder.Messages[1]); }
public void Test_One_Village_With_Building_Being_Built() { var testVillage = PrepareTestOneVillage(); var resultVillage = _serviceProviderBuilder.Mapper.Map <Village>(testVillage); var finishTime = TimeSpan.FromMinutes(30); resultVillage.Dorf1BuildTimeLeft = finishTime; resultVillage.CanBuild = false; resultVillage.Resources = new Resources { Lumber = 100, Clay = 100, Iron = 100, Crop = 100 }; resultVillage.Warehourse = 800; resultVillage.Granary = 800; var infoResult = Builder <BaseScenarioResult> .CreateNew() .With(x => x.Success = true) .With(x => x.Villages = new List <Village> { resultVillage }) .Build(); var gamePlayMock = new Mock <IGameplayClient>(); gamePlayMock.Setup(x => x.ExecuteActions(It.IsAny <TravianUser>(), It.IsAny <IEnumerable <GameAction> >())) .Returns(Task.FromResult(infoResult)); gamePlayMock.Setup(x => x.ExecuteActions(It.IsAny <TravianUser>(), It.IsAny <IEnumerable <BuildAction> >())) .Returns(Task.FromResult(new BaseScenarioResult())); _serviceProviderBuilder.WithService(gamePlayMock.Object); var cmd = new FakeCommand(); var cmdMock = new Mock <ICommandFactory>(); cmdMock.Setup(x => x.GetQueueableCommand(nameof(BuildCommand), It.IsAny <long>())) .Returns(cmd); _serviceProviderBuilder.WithService(cmdMock.Object); var job = new BuildingPlanExecutionJob(_serviceProviderBuilder.Build()); var data = new JobExecutionData { TravianUser = FakeDataProvider.GetUser(PlayerStatus.ALL_QUIET, true), JobType = typeof(BuildingPlanExecutionJob) }; var now = DateTimeOffset.Now; Assert.DoesNotThrowAsync(async() => await job.Execute(data)); _serviceProviderBuilder.LoggerMock.Verify(x => x.Log( LogLevel.Error, It.IsAny <EventId>(), It.IsAny <It.IsAnyType>(), It.IsAny <Exception>(), (Func <It.IsAnyType, Exception, string>)It.IsAny <object>()), Times.Never); gamePlayMock.Verify(x => x.ExecuteActions(It.IsAny <TravianUser>(), It.IsAny <IEnumerable <GameAction> >()), Times.Once); gamePlayMock.Verify(x => x.ExecuteActions(It.IsAny <TravianUser>(), It.IsAny <IEnumerable <BuildAction> >()), Times.Never); var diff = cmd.Start - now - finishTime; Assert.IsTrue(diff <= TimeSpan.FromSeconds(3)); // will fail while debugging because of date time now Assert.IsTrue(diff >= TimeSpan.FromSeconds(2)); }