예제 #1
0
        public async Task Update_given_existing_track_Updates_properties()
        {
            var context = new Mock <ISlotCarContext>();
            var entity  = new Track {
                Id = 42
            };

            context.Setup(c => c.Tracks.FindAsync(42)).ReturnsAsync(entity);

            using (var repository = new TrackRepository(context.Object))
            {
                var track = new TrackUpdateDTO
                {
                    Id             = 42,
                    Name           = "name",
                    LengthInMeters = 12.5,
                    BestLap        = TimeSpan.FromSeconds(6.3),
                    MaxCars        = 4
                };

                await repository.Update(track);
            }

            Assert.Equal("name", entity.Name);
            Assert.Equal(12.5, entity.LengthInMeters);
            Assert.Equal(TimeSpan.FromSeconds(6.3).Ticks, entity.BestLapInTicks);
            Assert.Equal(4, entity.MaxCars);
        }
예제 #2
0
        public async void putSucces()
        {
            var track = new TrackUpdateDTO()
            {
                Id = 12121
            };
            var mockRepository = new Mock <ITrackRepository>();

            mockRepository.Setup(a => a.Update(track)).ReturnsAsync(true);
            var controller = new TracksController(mockRepository.Object);
            var response   = await controller.Put(12121, track) as OkObjectResult;

            Assert.Equal(track, response.Value);
        }
예제 #3
0
        public async void putWithNotFound()
        {
            var track = new TrackUpdateDTO()
            {
                Id = 12121
            };
            var mockRepository = new Mock <ITrackRepository>();

            mockRepository.Setup(a => a.Update(track)).ReturnsAsync(false);
            var controller = new TracksController(mockRepository.Object);
            var response   = await controller.Put(12121, track);

            Assert.IsType <NotFoundResult>(response);
        }
예제 #4
0
        public async void putWithBadModelState()
        {
            var track = new TrackUpdateDTO()
            {
            };
            var mockRepository = new Mock <ITrackRepository>();
            var controller     = new TracksController(mockRepository.Object);

            controller.ModelState.AddModelError(string.Empty, "Error");

            var response = await controller.Put(0, track);

            Assert.IsType <BadRequestObjectResult>(response);
        }
예제 #5
0
        public async Task <IActionResult> Put(int id, [FromBody] TrackUpdateDTO trackUpdateDTO)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            trackUpdateDTO.Id = id;

            var update = await _repository.Update(trackUpdateDTO);

            if (!update)
            {
                return(NotFound());
            }
            return(Ok(update));
        }
예제 #6
0
        public async Task Update_given_non_existing_track_does_not_call_SaveChangesAsync()
        {
            var context = new Mock <ISlotCarContext>();

            context.Setup(c => c.Tracks.FindAsync(42)).ReturnsAsync(default(Track));

            using (var repository = new TrackRepository(context.Object))
            {
                var track = new TrackUpdateDTO {
                    Id = 42
                };

                await repository.Update(track);
            }

            context.Verify(c => c.SaveChangesAsync(default(CancellationToken)), Times.Never);
        }
예제 #7
0
        public async Task Update_given_non_existing_track_returns_false()
        {
            var context = new Mock <ISlotCarContext>();

            context.Setup(c => c.Tracks.FindAsync(42)).ReturnsAsync(default(Track));

            using (var repository = new TrackRepository(context.Object))
            {
                var track = new TrackUpdateDTO {
                    Id = 42
                };

                var success = await repository.Update(track);

                Assert.False(success);
            }
        }
예제 #8
0
        public async Task <IActionResult> Put(int id, [FromBody] TrackUpdateDTO trackUpdateDTO)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            bool IsThere = await repository.Update(trackUpdateDTO);

            if (!IsThere)
            {
                return(NotFound());
            }
            else
            {
                return(Ok(trackUpdateDTO));
            }
        }
예제 #9
0
        public async Task Update_given_id_updates_track()
        {
            var track = new TrackCreateDTO()
            {
                Name = "Awesome Track"
            };
            var updatedTrack = new TrackUpdateDTO()
            {
                Name = "Awesome Track", MaxCars = 12, LengthInMeters = 5000
            };

            var repo = new TrackRepository(context);
            await repo.Create(track);

            var controller = new TracksController(repo);
            var put        = await controller.Put(1, updatedTrack) as OkObjectResult;

            var actual = await repo.Find(1);

            Assert.Equal(updatedTrack.MaxCars, actual.MaxCars);
            Assert.Equal(updatedTrack.LengthInMeters, actual.LengthInMeters);
        }