Save a new post
This commit is contained in:
parent
71bddddc13
commit
a85c12f91c
5 changed files with 101 additions and 3 deletions
|
@ -5,12 +5,18 @@ using Microsoft.Extensions.Logging;
|
|||
using NSubstitute;
|
||||
using NSubstitute.ExceptionExtensions;
|
||||
using Robware.Api.Blog.Controllers;
|
||||
using Robware.Api.Blog.Models;
|
||||
using Robware.Blog;
|
||||
using Robware.Data;
|
||||
using Xunit;
|
||||
|
||||
namespace Robware.Api.Blog.Tests {
|
||||
public class BlogControllerTests {
|
||||
private bool ShouldBeEquivalentTo<T>(T post, T expected) {
|
||||
post.Should().BeEquivalentTo(expected);
|
||||
return true;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Get_WithUrl_ReturnsBlogPost() {
|
||||
var logger = Substitute.For<ILogger<BlogController>>();
|
||||
|
@ -154,5 +160,68 @@ namespace Robware.Api.Blog.Tests {
|
|||
var controller = new BlogController(logger, repo);
|
||||
(await controller.GetCount()).Value.Should().Be(0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Save_WithNewSubmission_Returns200() {
|
||||
var logger = Substitute.For<ILogger<BlogController>>();
|
||||
var repo = Substitute.For<IBlogRepository>();
|
||||
|
||||
var submission = new BlogPostSubmission {
|
||||
Content = "content",
|
||||
Title = "title"
|
||||
};
|
||||
|
||||
var controller = new BlogController(logger, repo);
|
||||
(await controller.SavePost(submission)).Should().BeOfType<OkResult>();
|
||||
|
||||
var expected = new BlogPost {
|
||||
Title = "title",
|
||||
Draft = "content",
|
||||
Url = "title"
|
||||
};
|
||||
|
||||
await repo.Received(1).SavePost(Arg.Is<BlogPost>(post => ShouldBeEquivalentTo(post, expected)));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Save_WithExistingSubmission_Returns200() {
|
||||
var logger = Substitute.For<ILogger<BlogController>>();
|
||||
var repo = Substitute.For<IBlogRepository>();
|
||||
repo.GetPostByIdAsync(1).Returns(new BlogPost {Content = "old content"});
|
||||
|
||||
var submission = new BlogPostSubmission {
|
||||
Id = 1,
|
||||
Content = "new content",
|
||||
Title = "title"
|
||||
};
|
||||
|
||||
var controller = new BlogController(logger, repo);
|
||||
(await controller.SavePost(submission)).Should().BeOfType<OkResult>();
|
||||
|
||||
var expected = new BlogPost {
|
||||
Title = "title",
|
||||
Content = "old content",
|
||||
Draft = "new content",
|
||||
Url = "title"
|
||||
};
|
||||
|
||||
await repo.Received(1).SavePost(Arg.Is<BlogPost>(post => ShouldBeEquivalentTo(post, expected)));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Save_WithExistingSubmission_WherePostDoesntExist_Returns400() {
|
||||
var logger = Substitute.For<ILogger<BlogController>>();
|
||||
var repo = Substitute.For<IBlogRepository>();
|
||||
repo.GetPostByIdAsync(1).Throws(new ItemNotFoundException("", null));
|
||||
|
||||
var submission = new BlogPostSubmission {
|
||||
Id = 1,
|
||||
Content = "new content",
|
||||
Title = "title"
|
||||
};
|
||||
|
||||
var controller = new BlogController(logger, repo);
|
||||
(await controller.SavePost(submission)).Should().BeOfType<BadRequestObjectResult>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue