コード例 #1
0
        public void Save_UpdatePost()
        {
            using (var fixture = new FixtureInit("http://localhost"))
            {
                //arrange
                var repository = new BlogPostsRepository(fixture.Setup.Context);
                var post = new BlogPost
                {
                    Url = "Some-new-post",
                    Title = "Some new post",
                    Body = "<p>This is new post in blog</p>",
                    CreatedBy = "AlexanderB",
                    CreatedDate = DateTime.Now
                };

                repository.Save(post);

                var blogPost = repository.BlogPosts.Where(p => p.Url == post.Url).Single();
                
                // act
                blogPost.Title += "(updated)";
                repository.Save(blogPost);

                //assert
                var updatedPost = repository.BlogPosts.Where(p => p.Url == post.Url).Single();
                Assert.That(updatedPost.Title, Is.EqualTo("Some new post(updated)"));
            }
        }
コード例 #2
0
        public void Save(BlogPost blogPost)
        {
            if (blogPost.Id == 0 && _context.BlogPosts.WithUrl(blogPost.Url) != null)
            {
                throw new DuplicateKeyException(blogPost, "Blog post with the same URL already exists. Please correct blog post title.");
            }

            if (blogPost.Id == 0)
            {
                _context.BlogPosts.InsertOnSubmit(blogPost);
            }
            _context.SubmitChanges();
        }
コード例 #3
0
        private static void SubmitTenBlogpostsToRepository(BlogPostsRepository repository)
        {
            for (var blog = 0; blog < 10; blog++)
            {
                var post = new BlogPost
                {
                    Url = "Url-" + blog,
                    Title = "Some new post: " + blog,
                    Body = "<p>This is new post in blog</p>",
                    CreatedBy = "AlexanderB",
                    CreatedDate = (DateTime.Now.AddDays(blog))
                };

                repository.Save(post);
            }
        }
コード例 #4
0
        public void SaveBlogPost()
        {
            using (var fixture = new FixtureInit("http://localhost"))
            {
                //arrange
                var repository = new BlogPostsRepository(fixture.Setup.Context);
                var post = new BlogPost
                {
                    Url = "Some-new-post",
                    Title = "Some new post",
                    Body = "<p>This is new post in blog</p>",
                    CreatedBy = "AlexanderB",
                    CreatedDate = DateTime.Now
                };

                //act
                repository.Save(post);

                //assert
                Assert.That(post.Id, Is.GreaterThan(0));
            }
        }
コード例 #5
0
        private static Mock<IBlogPostsRepository> CreateRepositoryMock(int blogPostsCount = 10)
        {
            var blogRepositoryMock = new Mock<IBlogPostsRepository>();

            var posts = new List<BlogPost>();
            for (var blog = 0; blog < blogPostsCount; blog++)
            {
                var post = new BlogPost
                {
                    Url = "Url-" + blog,
                    Title = "Some new post: " + blog,
                    Body = "<p>This is new post in blog</p>",
                    CreatedDate = (DateTime.Now.AddDays(blog))
                };

                posts.Add(post);
            }

            blogRepositoryMock.Setup(b => b.BlogPosts).Returns(posts.AsQueryable());

            return blogRepositoryMock;
        }
コード例 #6
0
 public BlogPostProcessor(BlogPost post)
 {
     _post = post;
 }
コード例 #7
0
        public static string GetPostBody(BlogPost post)
        {
            var processor = new BlogPostProcessor(post);

            return processor.GetPostBody();
        }
コード例 #8
0
        public void DeletePost()
        {
            using (var fixture = new FixtureInit("http://localhost"))
            {
                //arrange
                var repository = new BlogPostsRepository(fixture.Setup.Context);
                var post = new BlogPost
                {
                    Url = "Some-new-post",
                    Title = "Some new post",
                    Body = "<p>This is new post in blog</p>",
                    CreatedBy = "AlexanderB",
                    CreatedDate = DateTime.Now
                };

                repository.Save(post);

                //act
                repository.Delete(post);

                //assert
                var foundTask = repository.BlogPosts.WithId(post.Id);
                Assert.That(foundTask, Is.Null);
            }
        }
コード例 #9
0
        public void AddPost_Post_UrlForPost_Is_Created()
        {
            //arrange
            var blogRepository = new Mock<IBlogPostsRepository>();
            var submittedPosts = new List<BlogPost>();
            blogRepository.Setup(b => b.Save(It.IsAny<BlogPost>())).Callback((BlogPost p) => submittedPosts.Add(p));

            var blogManagement = new BlogManagementController(blogRepository.Object);
            var model = new BlogPost { Title = "Hey Joe" };
            
            //act
            var result = blogManagement.AddPost(model) as ViewResult;

            //post
            var post = submittedPosts.First();
            Assert.That(model.Url, Is.EqualTo("hey-joe"));
        }
コード例 #10
0
        public void AddPost_Post_Url_Contains_No_Punctuation()
        {
            //arrange
            var blogRepository = new Mock<IBlogPostsRepository>();
            var submittedPosts = new List<BlogPost>();
            blogRepository.Setup(b => b.Save(It.IsAny<BlogPost>()))
                .Callback((BlogPost p) =>
                {
                    if (submittedPosts.Find(x => x.Url == p.Url) != null)
                    {
                        throw new Exception();
                    }
                    submittedPosts.Add(p);
                }
                );

            var blogManagement = new BlogManagementController(blogRepository.Object);
            var model = new BlogPost { Title = "Hey, Joe.!:;" };

            //act
            var result = blogManagement.AddPost(model) as ViewResult;

            //post
            var post = submittedPosts.First();
            Assert.That(model.Url, Is.EqualTo("hey-joe"));
        }
コード例 #11
0
        public void AddPost_Post_CreatedDate_Contains_HourMinutesSeconds()
        {
            //arrange
            var blogRepository = new Mock<IBlogPostsRepository>();
            var submittedPosts = new List<BlogPost>();
            blogRepository.Setup(b => b.Save(It.IsAny<BlogPost>()))
                .Callback((BlogPost p) =>
                {
                    if (submittedPosts.Find(x => x.Url == p.Url) != null)
                    {
                        throw new Exception();
                    }
                    submittedPosts.Add(p);
                }
                );

            var blogManagement = new BlogManagementController(blogRepository.Object);
            var model = new BlogPost { Title = "Hey Joe" };

            //act
            var result = blogManagement.AddPost(model) as ViewResult;

            //post
            var post = submittedPosts.First();
            Assert.That(post.CreatedDate, Is.Not.Null);
            Assert.That(post.CreatedDate.ToLongTimeString(), Is.Not.EqualTo("00:00:00"));
        }
コード例 #12
0
        public void AddPost_Post_NotAddPost_With_SameUrl()
        {
            //arrange
            var blogRepository = new Mock<IBlogPostsRepository>();
            var submittedPosts = new List<BlogPost>();
            blogRepository.Setup(b => b.Save(It.IsAny<BlogPost>()))
                .Callback((BlogPost p) => 
                {
                    if (submittedPosts.Find(x => x.Url == p.Url) != null)
                    {
                        throw new Exception();
                    }
                    submittedPosts.Add(p); 
                }
                );

            var blogManagement = new BlogManagementController(blogRepository.Object);
            var model = new BlogPost { Title = "Hey Joe" };
            
            //act
            var result = blogManagement.AddPost(model) as ViewResult;
            result = blogManagement.AddPost(model) as ViewResult;

            //post
            Assert.That(blogManagement.ModelState[""].Errors[0].ErrorMessage, Is.EqualTo("Post could not be added. Exception of type 'System.Exception' was thrown."));
        }
コード例 #13
0
 partial void DeleteBlogPost(BlogPost instance);
コード例 #14
0
 partial void UpdateBlogPost(BlogPost instance);
コード例 #15
0
 partial void InsertBlogPost(BlogPost instance);