Пример #1
0
        public async Task<ActionResult> Create(EventViewModel eventViewModel)
        {
            if (!ModelState.IsValid)
            {
                return View(eventViewModel);
            }

            Event @event = new Event
            {
                Date = eventViewModel.DateTime,
                Description = eventViewModel.Description,
                Duration = eventViewModel.Duration,
                Name = eventViewModel.Name
            };

            db.Events.Add(@event);
            await db.SaveChangesAsync();

            this.AddAlert(AlertType.Success, $"Successfully created the \"{@event.Name}\" event.");

            return RedirectToAction("Details", new { id = @event.Id });
        }
Пример #2
0
        public async Task<ActionResult> Edit(Guid? id)
        {
            if (id == null)
            {
                throw new HttpException(NotFound, nameof(Event));
            }

            Event item = await db.Events.FindAsync(id);

            if (item == null)
            {
                throw new HttpException(NotFound, nameof(Event));
            }

            EventViewModel viewModel = new EventViewModel
            {
                Date = item.Date.Date,
                Description = item.Description,
                Duration = item.Duration,
                Id = item.Id,
                Name = item.Name,
                Time = item.Date
            };

            return View(viewModel);
        }
        public async void EditPOST_IdInDb_CallsSaveChangesAsync()
        {
            Event item = new Event
            {
                Id = Id,
            };

            EventViewModel viewModel = new EventViewModel
            {
                Id = item.Id,
            };

            Mock<IVeilDataAccess> dbMock = TestHelpers.GetVeilDataAccessFake();
            Mock<DbSet<Event>> eventDbSetStub = TestHelpers.GetFakeAsyncDbSet(new List<Event>().AsQueryable());
            eventDbSetStub.Setup(edb => edb.FindAsync(viewModel.Id)).ReturnsAsync(item);

            dbMock.Setup(db => db.Events).Returns(eventDbSetStub.Object);
            dbMock.Setup(db => db.SaveChangesAsync()).ReturnsAsync(1).Verifiable();

            EventsController controller = new EventsController(dbMock.Object, idGetter: null);

            await controller.Edit(viewModel);

            Assert.That(
                () =>
                    dbMock.Verify(db => db.SaveChangesAsync(),
                    Times.Exactly(1)),
                Throws.Nothing);
        }
        public async void EditPOST_UpdateSuccess_RedirectToDetailsForTheItem()
        {
            Event item = new Event
            {
                Id = Id,
            };

            EventViewModel viewModel = new EventViewModel
            {
                Id = item.Id,
            };

            Mock<IVeilDataAccess> dbStub = TestHelpers.GetVeilDataAccessFake();
            Mock<DbSet<Event>> eventDbSetStub = TestHelpers.GetFakeAsyncDbSet(new List<Event>().AsQueryable());
            eventDbSetStub.Setup(edb => edb.FindAsync(viewModel.Id)).ReturnsAsync(item);

            dbStub.Setup(db => db.Events).Returns(eventDbSetStub.Object);

            EventsController controller = new EventsController(dbStub.Object, idGetter: null);

            var result = await controller.Edit(viewModel) as RedirectToRouteResult;

            Assert.That(result != null);
            Assert.That(result.RouteValues["Id"], Is.EqualTo(item.Id));
            Assert.That(result.RouteValues["Action"], Is.EqualTo("Details"));
        }
        public async void EditPOST_IdInDb_UpdatesQueriedItemToMatchEditedViewModel()
        {
            Event item = new Event
            {
                Id = Id,
                Name = "An Event",
                Description = "A description",
                Date = new DateTime(635826779187565538L, DateTimeKind.Local),
                Duration = "A duration"
            };

            EventViewModel viewModel = new EventViewModel
            {
                Id = item.Id,
                Name = "Edit Name",
                Description = "Edit Description",
                Time = new DateTime(635826926597335093L, DateTimeKind.Local),
                Date = new DateTime(2015, 11, 9),
                Duration = "Edit Duration"
            };

            Mock<IVeilDataAccess> dbStub = TestHelpers.GetVeilDataAccessFake();
            Mock<DbSet<Event>> eventDbSetStub = TestHelpers.GetFakeAsyncDbSet(new List<Event>().AsQueryable());
            eventDbSetStub.Setup(edb => edb.FindAsync(viewModel.Id)).ReturnsAsync(item);

            dbStub.Setup(db => db.Events).Returns(eventDbSetStub.Object);

            EventsController controller = new EventsController(dbStub.Object, idGetter: null);

            await controller.Edit(viewModel);

            Assert.That(item.Id, Is.EqualTo(viewModel.Id));
            Assert.That(item.Name, Is.EqualTo(viewModel.Name));
            Assert.That(item.Description, Is.EqualTo(viewModel.Description));
            Assert.That(item.Date, Is.EqualTo(viewModel.DateTime));
            Assert.That(item.Duration, Is.EqualTo(viewModel.Duration));
        }
        public void EditPOST_IdNotInDb_Throws404Exception()
        {
            EventViewModel viewModel = new EventViewModel
            {
                Id = Id
            };

            Mock<IVeilDataAccess> dbStub = TestHelpers.GetVeilDataAccessFake();
            Mock<DbSet<Event>> eventDbSetStub = TestHelpers.GetFakeAsyncDbSet(new List<Event>().AsQueryable());
            eventDbSetStub.Setup(edb => edb.FindAsync(Id)).ReturnsAsync(null);

            dbStub.Setup(db => db.Events).Returns(eventDbSetStub.Object);

            EventsController controller = new EventsController(dbStub.Object, idGetter: null);

            Assert.That(async () => await controller.Edit(viewModel), Throws.InstanceOf<HttpException>().And.Matches<HttpException>(ex => ex.GetHttpCode() == 404));
        }
        public async void EditPOST_InvalidModelState_RedisplaysViewWithSameViewModel()
        {
            EventViewModel item = new EventViewModel
            {
                Id = Id
            };

            EventsController controller = new EventsController(veilDataAccess: null, idGetter: null);
            controller.ModelState.AddModelError("Name", "Name is required");

            var result = await controller.Edit(item) as ViewResult;

            Assert.That(result != null);
            Assert.That(result.Model, Is.InstanceOf<EventViewModel>());
            Assert.That(result.Model, Is.EqualTo(item));
        }
        public async void CreatePOST_DbSavedChagnesCalled_ConfirmsSavedChanges()
        {
            EventViewModel viewModel = new EventViewModel
            {
                Name = "New Name",
                Description = "New Description",
                Time = new DateTime(635827525788997554L, DateTimeKind.Local),
                Date = new DateTime(2015, 11, 10),
                Duration = "New Duration"
            };

            Mock<IVeilDataAccess> dbMock = TestHelpers.GetVeilDataAccessFake();
            Mock<DbSet<Event>> eventDbSetStub = TestHelpers.GetFakeAsyncDbSet(new List<Event>().AsQueryable());
            eventDbSetStub.Setup(edb => edb.Add(viewModel));

            dbMock.Setup(db => db.Events).Returns(eventDbSetStub.Object);
            dbMock.Setup(db => db.SaveChangesAsync()).ReturnsAsync(1).Verifiable();

            EventsController controller = new EventsController(dbMock.Object, idGetter: null);

            await controller.Create(viewModel);

            Assert.That(
                () =>
                    dbMock.Verify(db => db.SaveChangesAsync(),
                    Times.Exactly(1)),
                Throws.Nothing);
        }
        public async void CreatePOST_EventCreatedWithCorrectData_ItemFromDbMatchesSubmittedViewModel()
        {
            Event addedEvent = null;

            EventViewModel viewModel = new EventViewModel
            {
                Name = "New Name",
                Description = "New Description",
                Time = new DateTime(635827525788997554L, DateTimeKind.Local),
                Date = new DateTime(2015, 11, 10),
                Duration = "New Duration"
            };

            Mock<IVeilDataAccess> dbStub = TestHelpers.GetVeilDataAccessFake();

            dbStub.Setup(db => db.Events.Add(It.IsAny<Event>())).Callback<Event>((model) =>
            {
                addedEvent = model;
            });

            EventsController controller = new EventsController(dbStub.Object, idGetter: null);

            await controller.Create(viewModel);

            Assert.That(addedEvent != null);
            Assert.That(addedEvent.Name, Is.EqualTo(viewModel.Name));
            Assert.That(addedEvent.Description, Is.EqualTo(viewModel.Description));
            Assert.That(addedEvent.Date, Is.EqualTo(viewModel.DateTime));
            Assert.That(addedEvent.Duration, Is.EqualTo(viewModel.Duration));
        }