コード例 #1
0
        public async Task CreatingCommentsTest()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options;
            var dbContext = new ApplicationDbContext(options);

            var repository = new EfDeletableEntityRepository <Comment>(dbContext);
            var service    = new CommentsService(repository);

            await service.CreateCommentAsync(
                new CreateCommentsViewModel
            {
                Content   = "1",
                ProductId = 2,
                UserId    = Guid.NewGuid().ToString(),
            });

            await service.CreateCommentAsync(
                new CreateCommentsViewModel
            {
                Content   = "1",
                ProductId = 2,
                UserId    = Guid.NewGuid().ToString(),
            });

            await service.CreateCommentAsync(
                new CreateCommentsViewModel
            {
                Content   = "1",
                ProductId = 2,
                UserId    = Guid.NewGuid().ToString(),
            });

            Assert.Equal(3, service.GetCommentsCount());
        }
コード例 #2
0
        public async Task CreateCommentCorrectly()
        {
            var options    = new DbContextOptionsBuilder <ApplicationDbContext>().UseInMemoryDatabase(Guid.NewGuid().ToString());
            var repository = new EfDeletableEntityRepository <Comment>(new ApplicationDbContext(options.Options));

            var service = new CommentsService(repository);
            var userId  = "45645645";
            var newsId  = "1235876";

            await service.CreateCommentAsync("Ivan", userId, newsId);

            await service.CreateCommentAsync("Hello", userId, newsId);

            Assert.True(repository.All().All(x => x.NewsId == newsId && x.UserId == userId));
        }
コード例 #3
0
        public async Task TestCreateExerciseAsync_WithValidData_ShouldCreateExerciseCorrectly()
        {
            var context    = ApplicationDbContextInMemoryFactory.InitializeContext();
            var repository = new EfDeletableEntityRepository <Comment>(context);
            var service    = new CommentsService(repository);

            await service.CreateCommentAsync("Some Content", "asdf", "1234");

            var expected      = "Some Content";
            var targetComment = repository.All().Where(c => c.Content == expected).FirstOrDefault();

            Assert.Equal(expected, targetComment.Content);
        }
コード例 #4
0
        public async Task CreateCommentAsyncShouldReturnCorrectValuesWhenGivenValidInputWithParentId()
        {
            var commentsList = new List <Comment>();
            var episodesList = new List <Episode>();

            episodesList.Add(new Episode()
            {
                Webtoon = new Webtoon {
                    TitleNumber = "hello"
                },
                EpisodeNumber = "darkness",
                Id            = "test123"
            });
            var webtoonTitleNumber = "hello";
            var episodeNumber      = "darkness";
            var mockCommentsRepo   = new Mock <ICommentsRepository>();

            mockCommentsRepo.Setup(x => x.All()).Returns(commentsList.AsQueryable());
            mockCommentsRepo.Setup(x => x.AddAsync(It.IsAny <Comment>())).Callback((Comment comment) => commentsList.Add(comment));
            var mockEpisodesService = new Mock <IEpisodesService>();

            mockEpisodesService.Setup(x => x.GetEpisodeId(webtoonTitleNumber, episodeNumber))
            .Returns(episodesList.FirstOrDefault(e => e.Webtoon.TitleNumber == webtoonTitleNumber && e.EpisodeNumber == episodeNumber).Id);

            var service = new CommentsService(mockCommentsRepo.Object, mockEpisodesService.Object);

            commentsList.Add(new Comment()
            {
                Id = "test123"
            });

            var test = new CommentInputModel()
            {
                EpisodeNumber      = "darkness",
                UserComment        = "my old friend",
                WebtoonTitleNumber = "hello",
                ParentId           = "test123",
            };
            await service.CreateCommentAsync(test, "test");

            var comment = commentsList.FirstOrDefault(x => x.ParentId == "test123");

            Assert.Equal(2, commentsList.Count());
            Assert.Equal("test123", comment.EpisodeId);
            Assert.Equal("test", comment.CommentAuthorId);
            Assert.Equal("my old friend", comment.CommentInfo);
            Assert.Equal("test123", comment.ParentId);
        }
コード例 #5
0
        public async Task CreateCommentAsyncShouldThrowExceptionIfUserIsNull()
        {
            var options = new DbContextOptionsBuilder <MyCalisthenicAppDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
                          .Options;

            var dbContext = new MyCalisthenicAppDbContext(options);

            IHttpContextAccessor httpContextAccessor = new HttpContextAccessor();

            var usersService = new UsersService(httpContextAccessor, dbContext, null);


            var mockMapper = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile(new MyCalisthenicAppProfile());
            });

            var mapper = mockMapper.CreateMapper();

            var commentsService = new CommentsService(dbContext, mapper, usersService);

            var commentModel = new CommentInputViewModel
            {
                Text = CommentText,
            };

            var exception = await Assert.ThrowsAsync <NullReferenceException>(async() => await commentsService.CreateCommentAsync(PostId, commentModel));

            Assert.IsType <NullReferenceException>(exception);
        }