public async void GetAllLocations_ShouldReturnOk() { WebApiTestsHelper.Lock(); // Arrange SetUp(); var locations = new List <Location> { new Location { Id = 1, Name = "Warsaw", LocationSnapshots = new List <LocationSnapshot> { new LocationSnapshot(), new LocationSnapshot() } }, new Location { Id = 2, Name = "Lisbon", LocationSnapshots = new List <LocationSnapshot> { new LocationSnapshot() } } }; var locationDataService = Substitute.For <ILocationDataService>(); locationDataService.GetAllLocationsAsync() .Returns(_ => { var tcs = new TaskCompletionSource <IEnumerable <Location> >(); tcs.SetResult(locations); return(tcs.Task); }); // Act var sit = new LocationsController(locationDataService, Substitute.For <ILogger <LocationsController> >()); var result = await sit.GetAllLocations(); // Assert var dtos = WebApiTestsHelper.ExtractGenericCollectionFromActionResult <OkObjectResult, IEnumerable <LocationDto> >(result) .ToList(); for (var i = 0; i < dtos.Count; i++) { Assert.Equal(locations[i].Id, dtos[i].Id); Assert.Equal(locations[i].Name, dtos[i].Name); Assert.Equal(locations[i].LocationSnapshots.Count, dtos[i].SnapshotsCount); } WebApiTestsHelper.Unlock(); }
public async void GroupSnapshots_ShouldReturnOk() { WebApiTestsHelper.Lock(); // Arrange SetUp(); var snapshotGroups = new List <SnapshotGroup> { new SnapshotGroup { Name = "December 2018", SnapshotIds = new List <int> { 1, 2, 3 } }, new SnapshotGroup { Name = "January 2019", SnapshotIds = new List <int> { 4, 5, 6 } } }; Task <IEnumerable <SnapshotGroup> > dataFetchOperation() { var tcs = new TaskCompletionSource <IEnumerable <SnapshotGroup> >(); tcs.SetResult(snapshotGroups); return(tcs.Task); }; var snapshotDataService = Substitute.For <ILocationSnapshotDataService>(); snapshotDataService.ChooseGroupByOperation(Arg.Any <GroupByCriteria>()) .Returns(dataFetchOperation); // Act var sit = new SnapshotGroupsController(snapshotDataService, Substitute.For <ILogger <SnapshotGroupsController> >()); var result = await sit.GroupSnapshots(GroupByCriteria.CreatedDate); // Assert var dtos = WebApiTestsHelper.ExtractGenericCollectionFromActionResult <OkObjectResult, IEnumerable <SnapshotGroupDto> >(result) .ToList(); for (var i = 0; i < dtos.Count; i++) { Assert.Equal(snapshotGroups[i].Name, dtos[i].Name); Assert.Equal(snapshotGroups[i].SnapshotIds, dtos[i].SnapshotIds); } WebApiTestsHelper.Unlock(); }
public async void GetSnapshotsByIds_ShouldReturnOk() { WebApiTestsHelper.Lock(); // Arrange SetUp(); var snapshots = new List <LocationSnapshot> { new LocationSnapshot { Id = 1, LocationId = 1, Longitude = 56, Latitude = 49, Altitude = 1024, PictureFileName = "SomePic.jpg", DateCreated = new DateTime(2018, 9, 1) } }; var snapshotDataService = Substitute.For <ILocationSnapshotDataService>(); snapshotDataService.GetSnapshotsByIdsAsync(Arg.Any <IEnumerable <int> >()) .Returns(_ => { var tcs = new TaskCompletionSource <IEnumerable <LocationSnapshot> >(); tcs.SetResult(snapshots); return(tcs.Task); }); // Act var sit = new LocationSnapshotsController(snapshotDataService, Substitute.For <ILogger <LocationSnapshotsController> >()); var result = await sit.GetSnapshotsByIds(new[] { 1, 2, 3 }); // Assert var dtos = WebApiTestsHelper.ExtractGenericCollectionFromActionResult <OkObjectResult, IEnumerable <LocationSnapshotDto> >(result) .ToList(); Assert.Equal(snapshots[0].Id, dtos[0].Id); Assert.Equal(snapshots[0].LocationId, dtos[0].LocationId); Assert.Equal(snapshots[0].Longitude, dtos[0].Longitude); Assert.Equal(snapshots[0].Latitude, dtos[0].Latitude); Assert.Equal(snapshots[0].Altitude, dtos[0].Altitude); Assert.Equal(snapshots[0].PictureFileName, dtos[0].PictureFileName); Assert.Equal(snapshots[0].DateCreated, dtos[0].DateCreated); WebApiTestsHelper.Unlock(); }
public async void GetMiniatures_ShouldReturnOk() { // Arrange SetUp(); // Act var sit = CreateController(); var result = await sit.GetMiniatures(new List <int> { 1, 2, 3 }); // Assert var dtos = WebApiTestsHelper.ExtractGenericCollectionFromActionResult <OkObjectResult, ArrayList>(result); Assert.Equal(3, dtos.Count); await _imageService.Received().GetThumbnailAsync(@"Snapshot_Pictures\Barcelona1.jpg"); await _imageService.Received().GetThumbnailAsync(@"Snapshot_Pictures\Prague1.jpg"); await _imageService.Received().GetThumbnailAsync(@"Snapshot_Pictures\Frankfurt1.jpg"); }