Cześć czy te dwa testy dobrze testuja mój PostService. Są poprawne pod kątem standardów ?
[TestFixture]
public class PostServiceShould
{
[Test]
public void Add_New_Post_To_Context()
{
var mockSet = new Mock<DbSet<Post>>();
var mockCtx = new Mock<ApplicationDbContext>();
mockCtx.Setup(x => x.Posts).Returns(mockSet.Object);
PostService post = new PostService(mockCtx.Object);
post.AddPost(new Post());
mockCtx.Verify(s => s.Posts.Add(It.IsAny<Post>()), Times.Once());
mockCtx.Verify(c => c.SaveChanges(), Times.Once());
}
[Test]
public void Can_Short_Content_Post()
{
var mockSet = new Mock<DbSet<Post>>();
var mockCtx = new Mock<ApplicationDbContext>();
mockCtx.Setup(x => x.Posts).Returns(mockSet.Object);
PostService postService = new PostService(mockCtx.Object);
Post post = new Post();
post.Content = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
string result = postService.GetShortContent(post.Content);
string expected = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...";
Assert.AreEqual(expected, result);
}
}
public class PostService : IPostService
{
private ApplicationDbContext db;
public PostService(ApplicationDbContext _db)
{
db = _db;
}
public void AddPost(Post post)
{
post.DateOfAddition = DateTime.Now;
post.ShortContent = GetShortContent(post.Content);
db.Posts.Add(post);
db.SaveChanges();
}
public string GetShortContent(string content)
{
string result = "";
if(content == null)
{
return result;
}
else
{
if (content.Length > 40)
{
result = content.Substring(0, 40);
result = result + "...";
return result;
}
else if (content.Length <= 40)
{
result = content;
}
return result;
}
}