public async Task AddVideo_WithProperVideo_ShouldAddCorrectly() { // Arrange var videoModel = new VideoCreationBindingModel() { Title = videoTitle, Slug = videoSlug, Description = videoDescription, YoutubeId = videoYoutubeId, PublishDateTime = DateTime.Now, UserId = videoUserId }; // Act await this.service.AddVideoAsync(videoModel); var video = this.dbContext.Videos.First(); // Assert Assert.AreEqual(1, this.dbContext.Videos.Count()); Assert.AreEqual(videoTitle, video.Title); Assert.AreEqual(videoSlug, video.Slug); Assert.AreEqual(videoDescription, video.Description); Assert.AreEqual("orp7WHibnaU", video.YoutubeId); Assert.AreEqual(videoUserId, video.UserId); }
public async Task <Video> AddVideoAsync(VideoCreationBindingModel model) { Validator.EnsureNotNull(model, ValidationConstants.VideoDefinedMessage); Validator.EnsureStringNotNullOrEmpty(model.Title, ValidationConstants.VideoTitleMessage); Validator.EnsureStringNotNullOrEmpty(model.Slug, ValidationConstants.VideoSlugMessage); Validator.EnsureStringNotNullOrEmpty(model.Description, ValidationConstants.VideoDescriptionMessage); Validator.EnsureStringNotNullOrEmpty(model.PublishDateTime.ToString(), ValidationConstants.VideoDateMessage); Validator.EnsureStringNotNullOrEmpty(model.UserId, ValidationConstants.VideoAuthorMessage); Validator.EnsureStringNotNullOrEmpty(model.YoutubeId, ValidationConstants.VideoYoutubeMessage); var video = this.Mapper.Map <Video>(model); string youtubeId = null; if (model.YoutubeId.Contains("youtube.com")) { youtubeId = model.YoutubeId.Split("v=").Last().Substring(0, 11); } else if (model.YoutubeId.Contains("youtu.be")) { youtubeId = model.YoutubeId.Split("/").Last().Substring(0, 11); } video.YoutubeId = youtubeId; await this.DbContext.Videos.AddAsync(video); await this.DbContext.SaveChangesAsync(); return(video); }
public async Task <IActionResult> Add(VideoCreationBindingModel model) { var video = await this.videosService.AddVideoAsync(model); SetSuccesfullMessage(AddedSuccessfully, Video); return(this.RedirectToAction("Details", new { id = video.Id, slug = video.Slug })); }
public async Task AddVideo_WithNullNull_ShouldThrowExeption() { // Arrange VideoCreationBindingModel videoModel = null; // Act Func <Task> addVideo = () => this.service.AddVideoAsync(videoModel); // Assert var exception = await Assert.ThrowsExceptionAsync <ArgumentException>(addVideo); Assert.AreEqual(ValidationConstants.VideoDefinedMessage, exception.Message); }
public async Task AddVideo_WithMissingSlug_ShouldThrowExeption() { // Arrange var videoModel = new VideoCreationBindingModel() { Title = videoTitle, Slug = null, Description = videoDescription, YoutubeId = videoYoutubeId, PublishDateTime = DateTime.Now, UserId = videoUserId }; // Act Func <Task> addVideo = () => this.service.AddVideoAsync(videoModel); // Assert var exception = await Assert.ThrowsExceptionAsync <ArgumentException>(addVideo); Assert.AreEqual(ValidationConstants.VideoSlugMessage, exception.Message); }
public void Add_WithValidVideo_ShouldCallService() { // Arrange var model = new VideoCreationBindingModel(); bool serviceCalled = false; var mockRepository = new Mock <IAdminVideosService>(); mockRepository.Setup(repo => repo.AddVideoAsync(model)) .Callback(() => serviceCalled = true); var userRepository = new Mock <IAdminUsersService>(); userRepository.Setup(repo => repo.GetAuthorsAsync()) .Callback(() => serviceCalled = true); var controller = new VideosController(userRepository.Object, mockRepository.Object); // Act var result = controller.Add(model); // Assert Assert.IsTrue(serviceCalled); }