public void CallingUploadPictureActionWithInocrrectModelShouldReturnViewResultWithModel() { this.LoginMock(true); var incorrectNewPicture = new UploadPictureBindingModel { PictureData = null, Title = "Title" }; this.meController.ModelState.AddModelError("PictureData", "Current value: null"); var result = this.meController.UploadPicture(incorrectNewPicture); Assert.IsInstanceOfType(result, typeof(ViewResult)); var viewResult = result as ViewResult; Assert.IsInstanceOfType(viewResult.Model, typeof(UploadPictureBindingModel)); }
public ActionResult UploadPicture(UploadPictureBindingModel model) { if (!this.ModelState.IsValid) { return this.View(model); } var userId = this.User.Identity.GetUserId(); var user = this.Data.Users.Find(userId); if (user == null) { this.RedirectToAction("Index", "Home"); } string thumbnailImageData = ""; try { var image = PictureUtills.CreateImageFromBase64(model.PictureData); var thumbnail = PictureUtills.CreateThumbnailFromImage(image, 316); thumbnailImageData = PictureUtills.ConvertImageToBase64(thumbnail); } catch (Exception ex) { thumbnailImageData = model.PictureData; } var picture = new Picture() { PictureData = model.PictureData, ThumbnailImageData = thumbnailImageData, Title = model.Title, AuthorId = userId, PostedOn = DateTime.Now, }; this.Data.Pictures.Add(picture); this.Data.SaveChanges(); return this.RedirectToAction("Pictures"); }
public void CallingUploadPictureActionWithCorrectUploadPictureBindingModelShouldShouldAddPicture() { this.LoginMock(true); var fakePicturesCountBeforeAdding = this.fakePictures.Count(); var newPicture = new UploadPictureBindingModel { PictureData = "base64", Title = "newPicture" }; var result = this.meController.UploadPicture(newPicture); Assert.IsInstanceOfType(result, typeof(RedirectToRouteResult)); var routeResult = result as RedirectToRouteResult; Assert.AreEqual(routeResult.RouteValues["action"], "Pictures"); var fakePicturesCountAfterAdding = this.mock.PictureRepositoryMock.Object.All().Count(); Assert.AreEqual(fakePicturesCountBeforeAdding + 1, fakePicturesCountAfterAdding); }