public async Task GetCreateShouldReturnViewWithValidModel() { // Arrange var userManager = this.GetUserManagerMock(); var controller = new ProgramsController(userManager.Object, null); // Act var result = await controller.Create(); // Assert result.Should().BeOfType <ViewResult>(); var model = result.As <ViewResult>().Model; model.Should().BeOfType <AddProgramFormModel>(); var formModel = model.As <AddProgramFormModel>(); formModel.StartDate.Year.Should().Be(DateTime.UtcNow.Year); formModel.StartDate.Month.Should().Be(DateTime.UtcNow.Month); formModel.StartDate.Day.Should().Be(DateTime.UtcNow.Day); var endDate = DateTime.UtcNow.AddDays(30); formModel.EndDate.Year.Should().Be(endDate.Year); formModel.EndDate.Month.Should().Be(endDate.Month); formModel.EndDate.Day.Should().Be(endDate.Day); this.AssertTrainersSelectList(formModel.Trainers); }
public void ProgramCRUDTest() { var account = App.Config.Content.ContentProviders[0].MediaServicesSets[0].MediaServicesAccounts[0]; var controller = new ProgramsController(); var context = account.GetContext(); var channel = context.Channels.FirstOrDefault(); int programCount = context.Programs.Count(); var name = string.Format("New-Program-{0}", DateTime.UtcNow.ToOADate().ToString().Replace(".", "-")); var settings = new ProgramSettings { Name = name, Description = name, ArchiveWindowLength = TimeSpan.FromHours(1) }; controller.Create(account.AccountName, channel.Id.NimbusIdToRawGuid(), settings); Assert.AreEqual(programCount + 1, context.Programs.Count()); var program = context.Programs.ToList().First(o => o.Name == name); //update the channel. var updateSettings = new ProgramUpdateSettings { Description = "SomeDescription" }; controller.Update(account.AccountName, program.Id.NimbusIdToRawGuid(), updateSettings); // create new context to avoid cache issue. context = account.GetContext(); program = context.Programs.Where(p => p.Id == program.Id).FirstOrDefault(); Assert.IsNotNull(program); Assert.AreEqual(updateSettings.Description, program.Description); controller.Delete(account.AccountName, program.Id.NimbusIdToRawGuid()); Assert.AreEqual(programCount, context.Programs.Count()); }
public async Task PostCreateShouldReturnViewWithCorrectModelWhenModelStateIsInvalid() { // Arrange var userManager = this.GetUserManagerMock(); var controller = new ProgramsController(userManager.Object, null); controller.ModelState.AddModelError(string.Empty, "Error"); // Act var result = await controller.Create(new AddProgramFormModel()); // Assert result.Should().BeOfType <ViewResult>(); var model = result.As <ViewResult>().Model; model.Should().BeOfType <AddProgramFormModel>(); var formModel = model.As <AddProgramFormModel>(); this.AssertTrainersSelectList(formModel.Trainers); }
public async Task PostCreateShouldReturnRedirectWithValidModel() { // Arrange const string nameValue = "Name"; const string descriptionValue = "Description"; var startDateValue = new DateTime(2000, 12, 10); var endDateValue = new DateTime(2000, 12, 15); const string trainerIdValue = "1"; string modelName = null; string modelDescription = null; int modelDaysPerWeek = 0; DateTime modelStartDate = DateTime.UtcNow; DateTime modelEndDate = DateTime.UtcNow; string modelTrainerId = null; string successMessage = null; var adminCourseService = new Mock <IAdminProgramsService>(); adminCourseService .Setup(c => c.CreateAsync( It.IsAny <string>(), It.IsAny <string>(), It.IsAny <int>(), It.IsAny <DateTime>(), It.IsAny <DateTime>(), It.IsAny <string>())) .Callback((string name, string description, int daysPerWeek, DateTime startDate, DateTime endDate, string trainerId) => { modelName = name; modelDescription = description; modelDaysPerWeek = daysPerWeek; modelStartDate = startDate; modelEndDate = endDate; modelTrainerId = trainerId; }) .Returns(Task.CompletedTask); var tempData = new Mock <ITempDataDictionary>(); tempData .SetupSet(t => t[WebConstants.TempDataSuccessMessageKey] = It.IsAny <string>()) .Callback((string key, object message) => successMessage = message as string); var controller = new ProgramsController(null, adminCourseService.Object); controller.TempData = tempData.Object; // Act var result = await controller.Create(new AddProgramFormModel { Name = nameValue, Description = descriptionValue, StartDate = startDateValue, EndDate = endDateValue, TrainerId = trainerIdValue }); // Assert modelName.Should().Be(nameValue); modelDescription.Should().Be(descriptionValue); modelStartDate.Should().Be(startDateValue); modelEndDate.Should().Be(endDateValue.AddDays(1)); modelTrainerId.Should().Be(trainerIdValue); successMessage.Should().Be($"Program {nameValue} created successfully!"); result.Should().BeOfType <RedirectToActionResult>(); result.As <RedirectToActionResult>().ActionName.Should().Be("Index"); result.As <RedirectToActionResult>().ControllerName.Should().Be("Home"); result.As <RedirectToActionResult>().RouteValues.Keys.Should().Contain("area"); result.As <RedirectToActionResult>().RouteValues.Values.Should().Contain(string.Empty); }