Пример #1
0
        public ActionResult RenderEventListItem(Event eventItem, bool onlyRegisteredEvents)
        {
            var model = new EventListItemViewModel
            {
                Event = eventItem,
                OnlyRegisteredEvents = onlyRegisteredEvents
            };

            Member currentMember = db.Members.Find(idGetter.GetUserId(User.Identity));

            if (currentMember != null)
            {
                model.CurrentMemberIsRegistered = currentMember.RegisteredEvents.Contains(model.Event);
            }

            return PartialView("_EventListItem", model);
        }
        public async void DeleteConfirmed_IdInDb_CallsSaveChangesOnTheDb()
        {
            Event item = new Event
            {
                Id = Id
            };

            Mock<IVeilDataAccess> dbMock = TestHelpers.GetVeilDataAccessFake();
            Mock<DbSet<Event>> eventDbSetStub =
                TestHelpers.GetFakeAsyncDbSet(new List<Event> { item }.AsQueryable());
            eventDbSetStub.Setup(edb => edb.FindAsync(item.Id)).ReturnsAsync(item);
            eventDbSetStub.Setup(edb => edb.Remove(item)).Returns(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.DeleteConfirmed(item.Id);

            Assert.That(
                () =>
                    dbMock.Verify(db => db.SaveChangesAsync(),
                    Times.Exactly(1)),
                Throws.Nothing);
        }
Пример #3
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 });
        }
        public async void Unregister_IdInDb_ReturnsMatchedEventAsModel()
        {
            Event eventItem = new Event
            {
                Id = Id,
                RegisteredMembers = new List<Member>()
            };

            Member member = new Member
            {
                UserId = UserId,
                RegisteredEvents = new List<Event>()
            };

            eventItem.RegisteredMembers.Add(member);
            member.RegisteredEvents.Add(eventItem);

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

            Mock<DbSet<Event>> eventDbSetStub = TestHelpers.GetFakeAsyncDbSet(new List<Event> { eventItem }.AsQueryable());
            eventDbSetStub.Setup(db => db.FindAsync(eventItem.Id)).ReturnsAsync(eventItem);
            dbStub.Setup(db => db.Events).Returns(eventDbSetStub.Object);

            Mock<DbSet<Member>> memberDbSetStub = TestHelpers.GetFakeAsyncDbSet(new List<Member> { member }.AsQueryable());
            memberDbSetStub.Setup(db => db.FindAsync(member.UserId)).ReturnsAsync(member);
            dbStub.Setup(db => db.Members).Returns(memberDbSetStub.Object);

            Mock<ControllerContext> context = new Mock<ControllerContext>();
            context.Setup(c => c.HttpContext.User.Identity).Returns<IIdentity>(null);

            Mock<IGuidUserIdGetter> idGetterStub = new Mock<IGuidUserIdGetter>();
            idGetterStub.Setup(id => id.GetUserId(It.IsAny<IIdentity>())).Returns(member.UserId);

            EventsController controller = new EventsController(dbStub.Object, idGetterStub.Object)
            {
                ControllerContext = context.Object
            };

            var result = await controller.Unregister(eventItem.Id) as ViewResult;

            Assert.That(result != null);
            Assert.That(result.Model, Is.InstanceOf<EventDetailsViewModel>());

            var model = (EventDetailsViewModel)result.Model;

            Assert.That(model.Event.Id, Is.EqualTo(eventItem.Id));
        }
        public async void Delete_IdInDb_ReturnsMatchedEventAsModel()
        {
            Event item = new Event
            {
                Id = Id
            };

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

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

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

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

            Assert.That(result != null);
            Assert.That(result.Model, Is.InstanceOf<Event>());

            var model = (Event) result.Model;

            Assert.That(model.Id, Is.EqualTo(item.Id));
        }
        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_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_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 async void EditGET_IdInDb_ReturnsViewModelWithMatchedEventDetails()
        {
            Event item = new Event
            {
                Id = Id,
                Date = new DateTime(635826779187565538L, DateTimeKind.Local),
                Description = "A Description",
                Duration = "Some duration",
                Name = "An event"
            };

            Mock<IVeilDataAccess> dbStub = TestHelpers.GetVeilDataAccessFake();
            Mock<DbSet<Event>> eventDbSetStub =
                TestHelpers.GetFakeAsyncDbSet(new List<Event> { item }.AsQueryable());

            eventDbSetStub.Setup(edb => edb.FindAsync(item.Id)).ReturnsAsync(item);

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

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

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

            Assert.That(result != null);
            Assert.That(result.Model, Is.InstanceOf<EventViewModel>());

            var model = (EventViewModel) result.Model;

            Assert.That(model.Id, Is.EqualTo(item.Id));
            Assert.That(model.Date, Is.EqualTo(item.Date.Date));
            Assert.That(model.Time.TimeOfDay, Is.EqualTo(item.Date.TimeOfDay));
            Assert.That(model.Description, Is.EqualTo(item.Description));
            Assert.That(model.Duration, Is.EqualTo(item.Duration));
            Assert.That(model.Name, Is.EqualTo(item.Name));
        }
        public void RenderEventListItem_MemberNotRegistered_ReturnsMatchingModel()
        {
            DateTime futureDate = DateTime.Now.AddDays(1);

            Event eventItem = new Event
            {
                Id = Id,
                Date = futureDate
            };

            Member member = new Member
            {
                UserId = UserId,
                RegisteredEvents = new List<Event>()
            };

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

            Mock<DbSet<Member>> memberDbSetStub = TestHelpers.GetFakeAsyncDbSet(new List<Member> { member }.AsQueryable());
            memberDbSetStub.Setup(db => db.Find(member.UserId)).Returns(member);
            dbStub.Setup(db => db.Members).Returns(memberDbSetStub.Object);

            Mock<ControllerContext> context = new Mock<ControllerContext>();
            context.Setup(c => c.HttpContext.User.Identity).Returns<IIdentity>(null);

            Mock<IGuidUserIdGetter> idGetterStub = new Mock<IGuidUserIdGetter>();
            idGetterStub.Setup(id => id.GetUserId(It.IsAny<IIdentity>())).Returns(member.UserId);

            EventsController controller = new EventsController(dbStub.Object, idGetterStub.Object)
            {
                ControllerContext = context.Object
            };

            var result = controller.RenderEventListItem(eventItem, false) as PartialViewResult;

            Assert.That(result != null);
            Assert.That(result.Model, Is.InstanceOf<EventListItemViewModel>());

            var model = (EventListItemViewModel)result.Model;

            Assert.That(model.Event.Id, Is.EqualTo(eventItem.Id));
        }