public void GetAll_EntitiesFound_ReturnsModels() { var queryModel = new EventGroupGetAllModel { EventParentId = 1 }; var entities = new List <EventGroup> { new EventGroup { Id = 1, EventParentId = queryModel.EventParentId.Value }, new EventGroup { Id = 2, EventParentId = queryModel.EventParentId.Value } }; var expected = new List <EventGroupModel> { new EventGroupModel { Id = 1 }, new EventGroupModel { Id = 2 } }; var contextMock = new Mock <IOnTaskDbContext>(); contextMock.Setup(x => x.GetEventGroups(It.IsAny <string>(), It.Is <int>(y => y == queryModel.EventParentId))).Returns(entities).Verifiable(); var mapperMock = new Mock <IMapperService>(); mapperMock.Setup(x => x.Map <EventGroupModel>(It.Is <EventGroup>(y => y == entities.First()), It.IsAny <string>())).Returns(expected.First()).Verifiable(); mapperMock.Setup(x => x.Map <EventGroupModel>(It.Is <EventGroup>(y => y == entities.Last()), It.IsAny <string>())).Returns(expected.Last()).Verifiable(); var target = InitializeTarget(contextMock.Object, mapperMock.Object); var actual = target.GetAll(queryModel).ToList(); contextMock.Verify(); Assert.IsTrue(actual.Any()); }
public void GetAll_ThrowsException_HandledByCatchBlock() { var queryModel = new EventGroupGetAllModel { EventParentId = default(int) }; var contextMock = new Mock <IOnTaskDbContext>(); contextMock.Setup(x => x.GetEventGroups(It.IsAny <string>(), It.IsAny <int>())).Throws <Exception>(); var target = InitializeTarget(contextMock.Object); target.GetAll(queryModel).ToList(); }
public void GetAll_NoEntitiesFound_ReturnsEmptyList() { var queryModel = new EventGroupGetAllModel { EventParentId = default(int) }; var entities = new List <EventGroup>(); var contextMock = new Mock <IOnTaskDbContext>(); contextMock.Setup(x => x.GetEventGroups(It.IsAny <string>(), It.Is <int>(y => y == queryModel.EventParentId))).Returns(entities).Verifiable(); var target = InitializeTarget(contextMock.Object); var results = target.GetAll(queryModel).ToList(); contextMock.Verify(); Assert.IsFalse(results.Any()); }
/// <summary> /// Gets <see cref="EventGroupModel"/> classes. /// </summary> /// <param name="model">The model which provides data on which <see cref="EventGroupModel"/> classes to get.</param> /// <returns>An <see cref="IEnumerable{T}"/> of all <see cref="EventGroupModel"/> classes.</returns> public IEnumerable <EventGroupModel> GetAll(EventGroupGetAllModel model) { try { return(context .GetEventGroups( ApplicationUser.Id, model.EventParentId) .Select(x => mapper.Map <EventGroupModel>(x)) .ToList()); } catch (Exception) { throw; } }
public IActionResult GetAll(EventGroupGetAllModel model) => Ok(service.GetAll(model));