public async Task BlogPostBuilder_BuildsBlogWithMetaTag() { var author = Author.Create(new User(""), new AuthorProfile("", "", ""), Mock.Of <IAuthorValidator>()); var mock = new Mock <IAuthorService>(); mock.Setup(a => a.GetAuthor()).Returns(author); var authorService = mock.Object; var imageService = new Mock <IImageService>().Object; var builder = new BlogPostBuilder(authorService, imageService); var tags = new List <MetaTag>() { new MetaTag("description", "test description") }; var addBlogPostCommand = new AddBlogPostCommand() { ContentIntro = "Intro", Content = "Test", Subject = "Subject" }; var category = new Category("", 1, ""); var blogPost = await builder.WrittenByCurrentAuthor().WithCategory(category).WithContent(addBlogPostCommand) .WithMetaTags(tags).Build(); Assert.Single(blogPost.MetaTags); }
/// <summary> /// Initializes a new instance of the <see cref="BlogPost"/> class /// using the given <see cref="BlogPostBuilder"/>. /// </summary> /// <param name="builder">The builder to use.</param> public BlogPost(BlogPostBuilder builder) { this.Id = builder.Id; this.Content = builder.Content; this.Created = builder.Created; this.Status = builder.Status; this.Tags = builder.Tags; }
/// <summary> /// Initializes a new instance of the <see cref="BlogPost"/> class. /// </summary> /// <param name="builder">The builder to create the post from.</param> /// <returns>A new instance of the <see cref="BlogPost"/> class.</returns> public static BlogPost Create(BlogPostBuilder builder) { if (builder is null) { throw new ArgumentNullException(nameof(builder)); } return(new BlogPost(builder)); }
/// <summary> /// Initialize a new <see cref="BlogPost"/> that is a draft. /// </summary> /// <returns>A draft <see cref="BlogPost"/>.</returns> internal static BlogPost Create() { string content = "# Something\nWords and things and such."; IClock clock = SystemClock.Instance; BlogPost blogPost = BlogPostBuilder.Create(clock) .WithId(Guid.NewGuid()) .WithContent(content) .Build(); return(blogPost); }
public void Given_tags_is_not_null_When_WithTags_is_called_Then_Tags_should_be_set() { // Arrange. IClock clock = ConstantClockStub.Create(0); BlogPostBuilder builder = BlogPostBuilder.Create(clock); IImmutableSet <Tag> tags = ImmutableHashSet.Create(Tag.Create("name")); // Act. builder = builder.WithTags(tags); // Assert. builder.Tags.Should().BeEquivalentTo(tags); }
public void Given_a_valid_Instant_When_WithCreated_is_called_Then_Created_Should_be_set() { // Arrange. IClock clock = ConstantClockStub.Create(0); BlogPostBuilder builder = BlogPostBuilder.Create(clock); Instant created = Instant.FromUnixTimeMilliseconds(100000); // Act. builder = builder.WithCreated(created); // Assert. builder.Created.Should().Be(created); }
public void Given_content_is_not_null_When_WithContent_is_called_Then_Content_should_be_set() { // Arrange. IClock clock = ConstantClockStub.Create(0); BlogPostBuilder builder = BlogPostBuilder.Create(clock); string content = "Content"; // Act. builder = builder.WithContent(content); // Assert. builder.Content.Should().Be(content); }
public void Given_status_is_not_null_When_WithStatus_is_called_Then_Status_should_be_set() { // Arrange. IClock clock = ConstantClockStub.Create(0); BlogPostBuilder builder = BlogPostBuilder.Create(clock); BlogPostStatus status = BlogPostStatus.Published; // Act. builder = builder.WithStatus(status); // Assert. builder.Status.Should().Be(status); }
public void Given_builder_is_null_When_Create_is_called_Then_a_ArgumentNullException_should_be_thrown() { // Given. BlogPost blogPost; BlogPostBuilder builder = null; // When. Action action = () => blogPost = BlogPost.Create(builder); // Then. action.Should().Throw <ArgumentNullException>() .WithMessage("*cannot be null*builder*"); }
public void Given_a_valid_guid_When_WithId_is_called_Then_Id_should_be_set() { // Arrange. IClock clock = ConstantClockStub.Create(0); BlogPostBuilder builder = BlogPostBuilder.Create(clock); Guid id = Guid.NewGuid(); // Act. builder = builder.WithId(id); // Assert. builder.Id.Should().Be(id); }
public void Given_null_IClock_When_Create_is_called_Then_an_ArgumentNullException_should_be_thrown() { // Arrange. IClock clock = null; // Act. Action createBuilder = () => BlogPostBuilder.Create(clock); // Assert. createBuilder.Should() .Throw <ArgumentNullException>() .WithMessage("*cannot be null*clock*"); }
public void Given_tags_is_null_When_WithTags_is_called_Then_an_ArgumentNullException_should_be_thrown() { // Arrange. IClock clock = ConstantClockStub.Create(0); BlogPostBuilder builder = BlogPostBuilder.Create(clock); IImmutableSet <Tag> tags = null; // Act. Action testCode = () => builder.WithTags(tags); // Assert. testCode.Should() .Throw <ArgumentNullException>() .WithMessage("*cannot be null*tags*"); }
public void Given_two_sets_of_tags_When_WithTags_is_called_Then_Tags_should_be_last_set_of_tags() { // Arrange. IClock clock = ConstantClockStub.Create(0); BlogPostBuilder builder = BlogPostBuilder.Create(clock); IImmutableSet <Tag> tags = ImmutableHashSet.Create(Tag.Create("name"), Tag.Create("pizza")); IImmutableSet <Tag> otherTags = ImmutableHashSet.Create(Tag.Create("name"), Tag.Create("age")); // Act. builder = builder.WithTags(tags) .WithTags(otherTags); // Assert. builder.Tags.Should().BeEquivalentTo(otherTags); }
public void Given_stauts_is_null_When_WithStatus_is_called_Then_an_ArgumentNullException_should_be_thrown() { // Arrange. IClock clock = ConstantClockStub.Create(0); BlogPostBuilder builder = BlogPostBuilder.Create(clock); BlogPostStatus status = null; // Act. Action testCode = () => builder.WithStatus(status); // Assert. testCode.Should() .Throw <ArgumentNullException>() .WithMessage("*cannot be null*status*"); }
public void Given_a_valid_IClock_When_Create_is_called_Then_builder_should_have_defaults_set() { // Arrange. BlogPostBuilder builder; IClock clock = ConstantClockStub.Create(0); // Act. builder = BlogPostBuilder.Create(clock); // Assert. builder.Id.Should().Be(Guid.Empty); builder.Content.Should().Be(string.Empty); builder.Created.Should().Be(clock.GetCurrentInstant()); builder.Status.Should().BeEquivalentTo(BlogPostStatus.Draft); builder.Tags.Should().BeEmpty(); }
public async Task AddBlogPostCommandHandler_AddsBlog() { var imageService = TestContext.CreateImageService(); var builder = new BlogPostBuilder(_authorService, imageService); var handlerContext = TestContext.CreateHandlerContext <BlogPostSummaryViewModel>(RequestDbContext, CreateMapper()); var addBlogPostCommandHandler = new AddBlogPostCommandHandler(handlerContext, builder); var category = await RequestDbContext.Categories.FirstOrDefaultAsync(); var addBlogPostCommand = new AddBlogPostCommand() { Subject = "Test", ContentIntro = "Test", Content = "Test", CategoryId = category.Id }; var result = await addBlogPostCommandHandler.Handle(addBlogPostCommand, CancellationToken.None); Assert.InRange(result.Id, 1, int.MaxValue); Assert.Equal(category.Name, result.Category); }
public async Task AddBlogPostCommandHandler_WithFile_AddsBlog() { var mapper = CreateMapper(); var imageFactory = TestContext.CreateImageService(); var builder = new BlogPostBuilder(_authorService, imageFactory); var handlerContext = TestContext.CreateHandlerContext <BlogPostSummaryViewModel>(RequestDbContext, mapper); var handler = new AddBlogPostCommandHandler(handlerContext, builder); var fileMock = TestContext.CreateFileMock(); var file = fileMock.Object; var category = await RequestDbContext.Categories.FirstOrDefaultAsync(); var message = new AddBlogPostCommand() { Subject = "Test", Content = "Test", ContentIntro = "Test", File = file, CategoryId = category.Id }; var result = await handler.Handle(message, CancellationToken.None); Assert.NotNull(result.Image.UriPath); }
public void Given_a_valid_BlogPostBuilder_When_Build_is_called_Then_a_matching_BlogPost_is_constructed() { // Arrange. IClock clock = ConstantClockStub.Create(0); BlogPostBuilder builder; BlogPost blogPost; Guid id = Guid.NewGuid(); const string content = "Content"; BlogPostStatus draft = BlogPostStatus.Draft; ImmutableHashSet <Tag> tags = ImmutableHashSet.Create(Tag.Create("tag")); builder = BlogPostBuilder.Create(clock) .WithId(id) .WithContent(content) .WithStatus(draft) .WithTags(tags); // Act. blogPost = builder.Build(); // Assert. blogPost.Should().BeEquivalentTo(builder); }
public AddBlogPostCommandHandler(HandlerContext <BlogPostSummaryViewModel> context, BlogPostBuilder builder) { _dbContext = context.DbContext; _context = context; _builder = builder; }