public void BlogPostController_Edit_GIVEN_BlogPostRepository_Contains_BlogPost_With_ID_1_WHEN_blogPostId_Is_1_THEN_Returns_ViewModel_For_BlogPost() { const int BlogPostId = 1; var imageReference = new ImageReference(1, "/noimage.jpg"); var author = new BlogUser { Id = 1, Forenames = "Joe", Surname = "Blogs" }; var blogPost = new BlogPost("Test", "This is the description", "Not much in this article", imageReference, DateTime.Now, author); blogPost.SetProperty("Id", BlogPostId); blogPost.UpdateSeries(new Series { Name = "Series A" }); blogPost.UpdateTags(new Tag[] { new Tag { Text = "Tag 1" } }); var mockedBlogPostRepository = new Mock<IBlogPostRepository>(); mockedBlogPostRepository.Setup(r => r.Load(BlogPostId)).Returns(blogPost); var controller = MockCreators.CreateBlogPostController(blogPostRepository: mockedBlogPostRepository.Object); var result = controller.Edit(blogPost.Id.Value) as ViewResult; result.ShouldNotBeNull(); result.Model.ShouldBeInstanceOfType(typeof(CreateEditModel)); var model = (CreateEditModel)result.Model; model.BlogPostId.ShouldEqual(blogPost.Id); model.PublishedDate.ShouldEqual(blogPost.PublishedDate); model.Title.ShouldEqual(blogPost.Title); model.Description.ShouldEqual(blogPost.Description); model.FullArticle.ShouldEqual(blogPost.FullArticle); model.ImageId.ShouldEqual(blogPost.ImageReference.Id.Value); model.SeriesName.ShouldEqual(blogPost.Series.Name); }
public void BlogPostController_List_GIVEN_BlogPost_Has_1_Item_THEN_Returns_1_ArticleListingViewModel_With_Data_From_Post() { const int BlogPostId = 1; var imageReference = new ImageReference(1, "/noimage.jpg"); var author = new BlogUser { Id = 1, Forenames = "Joe", Surname = "Blogs" }; var blogPost = new BlogPost("Test", "This is the description", "Not much in this article", imageReference, DateTime.Now, author); blogPost.SetProperty("Id", BlogPostId); blogPost.UpdateSeries(new Series { Name = "Series A" }); blogPost.UpdateTags(new Tag[] { new Tag { Text = "Tag 1" } }); var mockedBlogPostRepository = new Mock<IBlogPostRepository>(); mockedBlogPostRepository.Setup(r => r.LoadAll()).Returns(new BlogPost[] { blogPost }); var controller = MockCreators.CreateBlogPostController(blogPostRepository: mockedBlogPostRepository.Object); var result = controller.List() as ViewResult; result.ShouldNotBeNull(); result.Model.ShouldBeInstanceOfType(typeof(IEnumerable<BlogPostListingItemModel>)); var viewModelForPost = ((IEnumerable<BlogPostListingItemModel>)result.Model).First(); viewModelForPost.BlogPostId.ShouldEqual(blogPost.Id.Value); viewModelForPost.Title.ShouldEqual(blogPost.Title); viewModelForPost.PublishedDate.ShouldEqual(blogPost.PublishedDate); viewModelForPost.AuthorsName.ShouldEqual(blogPost.Author.FullName); }
public void BlogPostController_Edit_Returns_ViewResult() { const int BlogPostId = 1; var imageReference = new ImageReference(1, "/noimage.jpg"); var author = new BlogUser { Id = 1, Forenames = "Joe", Surname = "Blogs" }; var blogPost = new BlogPost("Test", "This is the description", "Not much in this article", imageReference, DateTime.Now, author); blogPost.SetProperty("Id", BlogPostId); blogPost.UpdateSeries(new Series { Name = "Series A" }); blogPost.UpdateTags(new Tag[] { new Tag { Text = "Tag 1" } }); var mockedBlogPostRepository = new Mock<IBlogPostRepository>(); mockedBlogPostRepository.Setup(r => r.Load(BlogPostId)).Returns(blogPost); var controller = MockCreators.CreateBlogPostController(blogPostRepository: mockedBlogPostRepository.Object); var result = controller.Edit(1); result.ShouldBeInstanceOfType(typeof(ViewResult)); }
public void BlogPostController_Save_WHEN_viewModel_Has_Id_THEN_Loads_Existing_Article_From_BlogPostRepository_Updates_And_Saves() { const int BlogPostId = 1; const int ImageId = 1; const int AuthorId = 1; const int OldSeriesId = 1; const int NewSeriesId = 2; const string NewSeriesName = "Series B"; const int OldTagId = 1; const int NewTagId = 2; Series newSeries = new Series{Name = NewSeriesName}; newSeries.SetProperty(s => s.Id, NewSeriesId); Tag oldTag = new Tag { Text = "Tag 1" }; oldTag.SetProperty(t => t.Id, OldTagId); Tag newTag = new Tag { Text = "Tag 2" }; newTag.SetProperty(t => t.Id, NewTagId); BlogPost savedBlogPost = null; var imageReference = new ImageReference(ImageId, "/noimage.jpg"); var author = new BlogUser { Id = AuthorId, Forenames = "Joe", Surname = "Blogs" }; var blogPost = new BlogPost("Test", "This is the description", "Not much in this article", imageReference, DateTime.Now, author); blogPost.SetProperty("Id", BlogPostId); var series = new Series { Name = "Series A" }; series.SetProperty(s => s.Id, OldSeriesId); blogPost.UpdateSeries(series); blogPost.UpdateTags(new Tag[] { new Tag { Text = "Tag 1" } }); var mockedBlogPostRepository = new Mock<IBlogPostRepository>(); mockedBlogPostRepository.Setup(r => r.LoadFullArticle(BlogPostId)).Returns(blogPost).Verifiable(); mockedBlogPostRepository.Setup(r => r.SaveOrUpdate(It.IsAny<BlogPost>())).Callback<BlogPost>(bp => savedBlogPost = bp); var mockedLookupRepository = MockCreators.CreateMockedLookupRepository(); mockedLookupRepository.Setup(r => r.LoadForId<ImageReference>(ImageId)).Returns(imageReference); mockedLookupRepository.Setup(r => r.LoadForDescription<Series>(It.IsAny<Expression<Func<Series, string>>>(), NewSeriesName)).Returns(newSeries); mockedLookupRepository.Setup(r => r.LoadForDescription<Tag>(It.IsAny<Expression<Func<Tag, string>>>(), oldTag.Text)).Returns(oldTag); mockedLookupRepository.Setup(r => r.LoadForDescription<Tag>(It.IsAny<Expression<Func<Tag, string>>>(), newTag.Text)).Returns(newTag); var controller = MockCreators.CreateBlogPostController(blogPostRepository: mockedBlogPostRepository.Object, lookupRepository: mockedLookupRepository.Object); var updatedBlogPost = new CreateEditModel { BlogPostId = BlogPostId, Description = "A different description", FullArticle = "A different Full Article", ImageId = ImageId, PublishedDate = blogPost.PublishedDate, SeriesName = NewSeriesName, TagTexts = "Tag 2", Title = blogPost.Title }; controller.Save(updatedBlogPost); mockedBlogPostRepository.Verify(r => r.LoadFullArticle(BlogPostId), Times.Once()); savedBlogPost.ShouldNotBeNull(); savedBlogPost.Id.ShouldEqual(BlogPostId); savedBlogPost.Description.ShouldEqual(updatedBlogPost.Description); savedBlogPost.FullArticle.ShouldEqual(updatedBlogPost.FullArticle); savedBlogPost.ImageReference.ShouldEqual(imageReference); savedBlogPost.PublishedDate.ShouldEqual(updatedBlogPost.PublishedDate.Value); savedBlogPost.Series.ShouldEqual(newSeries); savedBlogPost.Tags.ShouldContain((t) => t.TextEquals(newTag)); }
public void UpdateSeries_WHEN_series_parameter_Is_Null_THEN_Throws_ArgumentNullException() { var imageReference = GetImageReference(); var post = new BlogPost("ABC", "description", "fullText", imageReference, DateTime.Now, new BlogUser()); post.UpdateSeries(null); }
public void UpdateSeries_Sets_Series_Property_To_series_parameter() { var series = new Series { Name = "Test" }; var imageReference = GetImageReference(); var post = new BlogPost("ABC", "description", "fullText", imageReference, DateTime.Now, new BlogUser()); post.UpdateSeries(series); post.Series.ShouldEqual(series); }
private void UpdateSeriesOnBlogPost(BlogPost blogPost, string seriesName) { if (!String.IsNullOrWhiteSpace(seriesName) && !blogPost.SeriesNameEqual(seriesName)) { Series seriesFound = LookupRepository.LoadForDescription<Series>(s => s.Name, seriesName); if (seriesFound == null) seriesFound = new Series { Name = seriesName }; blogPost.UpdateSeries(seriesFound); } }