Exemplo n.º 1
0
        public async Task ReactMethodShouldAddReactionInDatabaseIfNotExistsAlready(string title, string description, ReactionType type)
        {
            var guid = Guid.NewGuid().ToString();

            var options = new DbContextOptionsBuilder <ForumDbContext>()
                          .UseInMemoryDatabase(guid)
                          .Options;

            var db = new ForumDbContext(options);
            var dateTimeProvider = new Mock <IDateTimeProvider>();

            dateTimeProvider.Setup(dtp => dtp.Now()).Returns(new DateTime(2020, 3, 27));

            var post = new Post
            {
                Id          = 1,
                Title       = title,
                Description = description,
                CategoryId  = 1,
                AuthorId    = guid,
                CreatedOn   = dateTimeProvider.Object.Now()
            };

            await db.Posts.AddAsync(post);

            await db.SaveChangesAsync();

            var postReactionsService = new PostReactionsService(db, dateTimeProvider.Object);
            var result = await postReactionsService.ReactAsync(type, 1, guid);

            var actual = await db.PostReactions.FirstOrDefaultAsync();

            var expected = new PostReaction
            {
                Id           = 1,
                PostId       = 1,
                Post         = post,
                AuthorId     = guid,
                ReactionType = type,
                CreatedOn    = dateTimeProvider.Object.Now()
            };

            actual.Should().BeEquivalentTo(expected);
            result.Should().BeOfType <ReactionsCountServiceModel>();
        }
Exemplo n.º 2
0
        public async Task ReactMethodShouldChangeReactionToNeutralIfReactionIsClickedTwice(ReactionType type)
        {
            var guid = Guid.NewGuid().ToString();

            var options = new DbContextOptionsBuilder <ForumDbContext>()
                          .UseInMemoryDatabase(guid)
                          .Options;

            var db = new ForumDbContext(options);
            var dateTimeProvider = new Mock <IDateTimeProvider>();

            dateTimeProvider.Setup(dtp => dtp.Now()).Returns(new DateTime(2020, 3, 27));

            var postReaction = new PostReaction
            {
                Id           = 1,
                PostId       = 1,
                AuthorId     = guid,
                ReactionType = type,
                CreatedOn    = dateTimeProvider.Object.Now()
            };

            await db.PostReactions.AddAsync(postReaction);

            await db.SaveChangesAsync();

            var postReactionsService = new PostReactionsService(db, dateTimeProvider.Object);
            var result = await postReactionsService.ReactAsync(type, 1, guid);

            var actual = await db.PostReactions.FirstOrDefaultAsync();

            var expected = new PostReaction
            {
                Id           = 1,
                PostId       = 1,
                AuthorId     = guid,
                ReactionType = ReactionType.Neutral,
                CreatedOn    = dateTimeProvider.Object.Now(),
                ModifiedOn   = dateTimeProvider.Object.Now()
            };

            actual.Should().BeEquivalentTo(expected);
            result.Should().BeOfType <ReactionsCountServiceModel>();
        }