public void AddPost_when_adding_an_tag_with_a_blank_tagname_should_throw_exception() { //Arrange SetupMocks(); Post post = new Post { Id = 0 }; post.AddTag(new Tag()); //Act service.AddPost(post); //Assert tagRepositoryMock.Verify(x => x.GetTagByName(It.IsAny<string>()), Times.Never()); postRepositoryMock.Verify(x => x.AddPost(It.IsAny<Post>()), Times.Never()); }
public void AddPost_when_adding_a_tag_that_already_exists_should_use_the_same_instance_of_the_tag() { //Arrange SetupMocks(); Tag newTag1 = new Tag { Id = 0, TagName = ".net" }; Tag newTag2 = new Tag { Id = 0, TagName = "misc" }; Post post = new Post(); post.AddTag(newTag1); post.AddTag(newTag2); //Act service.AddPost(post); //Assert Assert.IsNotNull(_AddedPost,"The post was not saved"); Assert.AreEqual(2, _AddedPost.Tags.Count); Tag tag1 = _AddedPost.Tags.Where(t => t.TagName == ".net").FirstOrDefault(); Tag tag2 = _AddedPost.Tags.Where(t => t.TagName == "misc").FirstOrDefault(); Assert.IsNotNull(tag1); Assert.IsNotNull(tag2); Assert.AreEqual(1, tag1.Id); Assert.AreEqual(2, tag2.Id); }
private void ProcessTagsInPost(Post post) { if (post.Tags == null || post.Tags.Count == 0) throw new ConstraintViolationException("Every post should have at least one tag attached to it"); IList<Tag> tags = new List<Tag>(post.Tags); foreach (var tag in tags) { if (string.IsNullOrEmpty(tag.TagName)) throw new ConstraintViolationException("Tags should not be empty"); //tag.Id = 0; if (tag.Id == 0) { if (tag.DateCreated < DateTime.Now) tag.DateCreated = DateTime.Now; tag.NormalizedTagName = StringUtility.GetNormalizedText(tag.TagName, '_'); post.Tags.Remove(tag); Tag tagFromRepo = tagRepository.GetTagByName(tag.TagName); post.AddTag(tagFromRepo ?? tag); } } }