public async Task TestUpdateAsync_WithIncorrectId_ShouldThrowNullRefEx()
        {
            var testActivityId         = "testActivityId";
            var incorrectActivityId    = "incorrectActivityId";
            var testInitialName        = "testInitialName";
            var testUpdatedName        = "testUpdatedName";
            var testInitialDescription = "testInitialDescription";
            var testUpdatedDescription = "testUpdatedDescription";

            var workoutActivity = new WorkoutActivity
            {
                Id          = testActivityId,
                Name        = testInitialName,
                Description = testInitialDescription,
                Icon        = "ActivityIconUrl",
            };

            await this.workoutActivitiesRepository.AddAsync(workoutActivity);

            await this.workoutActivitiesRepository.SaveChangesAsync();

            var workoutActivityEditInputModel = new WorkoutActivityEditInputModel()
            {
                Id          = incorrectActivityId,
                Name        = testUpdatedName,
                Description = testUpdatedDescription,
            };

            await Assert.ThrowsAsync <NullReferenceException>(async() => await this.workoutActivitiesService.UpdateAsync(workoutActivityEditInputModel));
        }
コード例 #2
0
        public async Task UpdateAsync(WorkoutActivityEditInputModel workoutActivityEditInputModel)
        {
            var workoutActivity = await this.workoutActivitiesRepository.All()
                                  .FirstOrDefaultAsync(x => x.Id == workoutActivityEditInputModel.Id);

            if (workoutActivity == null)
            {
                throw new NullReferenceException(string.Format(ServiceConstants.WorkoutActivity.NullReferenceActivityId, workoutActivityEditInputModel.Id));
            }

            var existingActivityWithSameName = await this.workoutActivitiesRepository.All()
                                               .FirstOrDefaultAsync(x => x.Name == workoutActivityEditInputModel.Name);

            if (existingActivityWithSameName != null)
            {
                if (existingActivityWithSameName.Id != workoutActivity.Id)
                {
                    throw new InvalidOperationException(string.Format(ServiceConstants.WorkoutActivity.SameNameActivityExists));
                }
            }

            workoutActivity.Name        = workoutActivityEditInputModel.Name;
            workoutActivity.Description = workoutActivityEditInputModel.Description;

            this.workoutActivitiesRepository.Update(workoutActivity);
            await this.workoutActivitiesRepository.SaveChangesAsync();
        }
        public async Task <IActionResult> Edit(WorkoutActivityEditInputModel workoutActivityEditInputModel)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(workoutActivityEditInputModel));
            }

            await this.workoutActivitiesService.UpdateAsync(workoutActivityEditInputModel);

            return(this.RedirectToAction(nameof(this.All)));
        }
        public async Task TestUpdateAsync_WithDuplicateName_ShouldThrowInvOpEx()
        {
            var testActivityId         = "testActivityId";
            var testInitialName        = "testInitialName";
            var testUpdatedName        = "testUpdatedName";
            var testInitialDescription = "testInitialDescription";
            var testUpdatedDescription = "testUpdatedDescription";

            await this.workoutActivitiesRepository.AddAsync(new WorkoutActivity()
            {
                Id          = "testId",
                Name        = testUpdatedName,
                Description = testUpdatedDescription,
            });

            await this.workoutActivitiesRepository.SaveChangesAsync();

            var workoutActivity = new WorkoutActivity
            {
                Id          = testActivityId,
                Name        = testInitialName,
                Description = testInitialDescription,
                Icon        = "ActivityIconUrl",
            };

            await this.workoutActivitiesRepository.AddAsync(workoutActivity);

            await this.workoutActivitiesRepository.SaveChangesAsync();

            var workoutActivityEditInputModel = new WorkoutActivityEditInputModel()
            {
                Id          = testActivityId,
                Name        = testUpdatedName,
                Description = testUpdatedDescription,
            };

            await Assert.ThrowsAsync <InvalidOperationException>(async() => await this.workoutActivitiesService.UpdateAsync(workoutActivityEditInputModel));
        }
        public async Task TestUpdateAsync_WithTestData_ShouldUpdateWorkoutActivity()
        {
            var testActivityId         = "testActivityId";
            var testInitialName        = "testInitialName";
            var testUpdatedName        = "testUpdatedName";
            var testInitialDescription = "testInitialDescription";
            var testUpdatedDescription = "testUpdatedDescription";

            var workoutActivity = new WorkoutActivity
            {
                Id          = testActivityId,
                Name        = testInitialName,
                Description = testInitialDescription,
                Icon        = "ActivityIconUrl",
            };

            await this.workoutActivitiesRepository.AddAsync(workoutActivity);

            await this.workoutActivitiesRepository.SaveChangesAsync();

            var workoutActivityEditInputModel = new WorkoutActivityEditInputModel()
            {
                Id          = testActivityId,
                Name        = testUpdatedName,
                Description = testUpdatedDescription,
            };

            await this.workoutActivitiesService.UpdateAsync(workoutActivityEditInputModel);

            var result = await this.workoutActivitiesRepository.All()
                         .Where(wa => wa.Id == testActivityId)
                         .FirstOrDefaultAsync();

            Assert.Equal(testUpdatedName, result.Name);
            Assert.Equal(testUpdatedDescription, result.Description);
        }