Esempio n. 1
0
        public async Task GetMetricsByPeriod_ReturnOk()
        {
            var request = new CpuMetricGetByPeriodQuery()
            {
                FromTime = DateTimeOffset.Now.AddDays(-5),
                ToTime   = DateTimeOffset.Now
            };

            _mockMediator.Setup(mediator => mediator.Send(It.IsAny <CpuMetricGetByPeriodQuery>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(new AgentCpuMetricResponse()
            {
                Metrics = new List <AgentCpuMetricDto>()
                {
                    new AgentCpuMetricDto()
                    {
                        Id    = 1,
                        Time  = DateTimeOffset.Now,
                        Value = 99
                    }
                }
            });

            var result = await _controller.GetMetricsByPeriod(request);

            var resultValue = ((OkObjectResult)result).Value as AgentCpuMetricResponse;

            _mockMediator.Verify(mediator => mediator.Send(It.Is <CpuMetricGetByPeriodQuery>(
                                                               m => m.FromTime == request.FromTime && m.ToTime == request.ToTime),
                                                           It.IsAny <CancellationToken>()), Times.Once);
            _mockMediator.Verify(mediator => mediator.Send(It.IsAny <CpuMetricGetByPeriodQuery>(), It.IsAny <CancellationToken>()), Times.Once);
            Assert.Single(resultValue.Metrics);
            Assert.Equal(1, resultValue.Metrics[0].Id);
            Assert.Equal(99, resultValue.Metrics[0].Value);
            Assert.IsAssignableFrom <IActionResult>(result);
        }