public async Task SongController_GetAdd_ShouldReturnViewWithValidModel() { //Arrenge var adminArtistService = this.GetAdminArtistServiceMock(); var controller = new SongsController(adminArtistService.Object, null); //Act var result = await controller.Add(); //Assert result.Should().BeOfType <ViewResult>(); var model = result.As <ViewResult>().Model; model.Should().BeOfType <SongFormViewModel>(); var formModel = model.As <SongFormViewModel>(); this.AssertArtistsSelectListItems(formModel.Artists); }
public async Task SongController_PostAdd_ShouldReturnViewWithCorrectMethodWhenModelStateIsInvalid() { //Arrenge var adminArtistService = this.GetAdminArtistServiceMock(); var adminSongService = this.GetAdminSongServiceBaseMock(); var controller = new SongsController(adminArtistService.Object, adminSongService.Object); controller.ModelState.AddModelError(string.Empty, "Error"); //Act var result = await controller.Add(new SongFormViewModel()); //Assert result.Should().BeOfType <ViewResult>(); var model = result.As <ViewResult>().Model; model.Should().BeOfType <SongFormViewModel>(); var formModel = model.As <SongFormViewModel>(); this.AssertArtistsSelectListItems(formModel.Artists); }
public async Task SongController_PostAdd_ShouldReturnRedirectWithValidModel() { //Arrenge string modelName = null; decimal modelPrice = 0; double modelDuration = 0; int modelArtistId = 0; Ganre modelGanre = 0; string successMessage = null; string resultModelName = "TestSong"; decimal resultModelPrice = 2; double resultModelDuration = 3; int resultModelArtistId = 4; Ganre resultModelGanre = Ganre.Disco; var adminSongService = this.GetAdminSongServiceBaseMock(); adminSongService .Setup(s => s.CreateAsync( It.IsAny <string>(), It.IsAny <decimal>(), It.IsAny <double>(), It.IsAny <int>(), It.IsAny <Ganre>())) .Callback((string name, decimal price, double duration, int artistId, Ganre ganre) => { modelName = name; modelPrice = price; modelDuration = duration; modelArtistId = artistId; modelGanre = ganre; }) .Returns(Task.CompletedTask); adminSongService .Setup(a => a.IsGanreExist(It.IsAny <int>())) .Returns(true); var adminArtistService = new Mock <IAdminArtistService>(); adminArtistService .Setup(a => a.ExistAsync(It.IsAny <int>())) .ReturnsAsync(true); var tempDate = new Mock <ITempDataDictionary>(); tempDate.SetupSet(t => t[WebConstants.TempDataSuccessMessageKey] = It.IsAny <string>()) .Callback((string key, object message) => successMessage = message as string); var controller = new SongsController(adminArtistService.Object, adminSongService.Object); controller.TempData = tempDate.Object; //Act var result = await controller.Add(new SongFormViewModel { Name = resultModelName, Price = resultModelPrice, Duration = resultModelDuration, ArtistId = resultModelArtistId, Ganre = resultModelGanre }); //Assert modelName.Should().Be(resultModelName); modelPrice.Should().Be(resultModelPrice); modelDuration.Should().Be(resultModelDuration); modelArtistId.Should().Be(resultModelArtistId); modelGanre.Should().Be(resultModelGanre); successMessage.Should().Be($"The song {resultModelName} has been added successfully"); result.Should().BeOfType <RedirectToActionResult>(); result.As <RedirectToActionResult>().ActionName.Should().Be("ListAll"); }