using FluentAssertions; using MongoDB.Driver; using NSubstitute; using Robware.Blog; using Robware.Blog.Data.MongoDB.State; using System; using System.Threading.Tasks; using Xunit; namespace Robware.Data.MongoDB.Tests { public class BlogRepositoryTests { [Fact] public async Task SavePost_WithNewPost_GetsNewIdAndSetsTimestampAndSavesAsync() { var collection = Substitute.For>(); collection.CountDocumentsAsync(Arg.Any>()).Returns(10); var database = Substitute.For(); database.GetCollection("blog").Returns(collection); var repo = new BlogRepository(database); var post = new BlogPost(); await repo.SavePost(post); post.Id.Should().Be(11); post.Timestamp.Should().BeCloseTo(DateTime.Now); await collection.Received(1).InsertOneAsync(Arg.Any()); } [Fact] public async Task SavePost_WithExistingPost_DoesNotSetTimestampAndSavesAsync() { var collection = Substitute.For>(); var database = Substitute.For(); database.GetCollection("blog").Returns(collection); var repo = new BlogRepository(database); var post = new BlogPost() { Id = 1 }; await repo.SavePost(post); post.Id.Should().Be(1); post.Timestamp.Should().Be(new DateTime()); await collection.Received(1).InsertOneAsync(Arg.Any()); } [Fact] public async Task GetCountAsync_ReturnsCountFromDatabase() { var collection = Substitute.For>(); collection.CountDocumentsAsync(Arg.Any>()).Returns(10); var database = Substitute.For(); database.GetCollection("blog").Returns(collection); var repo = new BlogRepository(database); (await repo.GetCountAsync()).Should().Be(10); } } }