Esempio n. 1
0
        public async Task CreatePost_Test()
        {
            _postAppService = new PostAppService(_postRepositoryMock, _userRepositoryMock);
            PostInput postDto = new PostInput()
            {
                AuthorId = UsingDbContext <User>(a => a.User.FirstOrDefault()).Id,
                Content  = "HI",
                Title    = "title5"
            };
            var post = new Post(postDto.AuthorId, postDto.Title, postDto.Content);

            //此时的_postRepositoryMock是模拟对象,用来断言
            //只要InsertAsync方法接收到这些参数值就返回post实例
            _postRepositoryMock.InsertAsync(Arg.Is <Post>(
                                                a => a.Content == post.Content &&
                                                a.AuthorId == post.AuthorId &&
                                                a.Title == post.Title)).Returns(post);

            //Act
            var flag = await _postAppService.CreatePost(postDto);

            //Assert
            //只要InsertAsync方法被调用,而且接收到参数值符合预期就断言通过
            await _postRepositoryMock.Received().InsertAsync(Arg.Is <Post>(
                                                                 a => a.Content == post.Content &&
                                                                 a.AuthorId == post.AuthorId &&
                                                                 a.Title == post.Title));

            this.Dispose();
        }
Esempio n. 2
0
        public async Task <JsonResponse> CreatePost(PostInput dto)
        {
            dto.AuthorId = UserInfo.Id;
            var Id = await _postAppService.CreatePost(dto);

            return(new JsonResponse(Id, 200));
        }
Esempio n. 3
0
 public IActionResult CreatePost([FromBody] Post post)
 {
     if (ModelState.IsValid)
     {
         var model = _postAppService.CreatePost(post);
         return(Ok(model));
     }
     return(NotFound());
 }
Esempio n. 4
0
        public async Task <IActionResult> CreatePost([FromForm] CreatePostDto input)
        {
            try
            {
                await _postAppService.CreatePost(input);

                return(Ok("Created Tenant"));
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
        public async Task Should_Insert_Post()
        {
            var title = Guid.NewGuid().ToString();

            await _postAppService.CreatePost(
                new CreatePostInput
            {
                CategoryId = 1,
                Content    = "a test content",
                Title      = title,
                Status     = PostStatus.Draft,
                Tags       = "test tags"
            });

            UsingDbContext(context => { context.Posts.FirstOrDefault(p => p.Title == title).ShouldNotBe(null); });
        }
Esempio n. 6
0
        public async Task <IActionResult> CreatePost([FromForm] CreatePostDto input)
        {
            var token  = GetToken();
            var userId = LoginHelper.GetClaim(token, "UserId");

            if (userId == null)
            {
                return(Unauthorized());
            }

            input.UserId = Guid.Parse(userId);

            var createdPost = await _postAppService.CreatePost(input);

            return(Ok(createdPost));
        }
 public async Task <IActionResult> OnPostAsync()
 {
     if (ModelState.IsValid)
     {
         string uniqueFileName = null;
         if (postDto.PhotoFile != null)
         {
             postDto.PhotoPath = CreatePhoto(postDto.PhotoFile);
         }
         else
         {
             ModelState.AddModelError(string.Empty, "Please choose a photo represent for this post.");
         }
         flag = await postService.CreatePost(postDto);
     }
     return(await this.OnGet());
 }
Esempio n. 8
0
        public async Task CreatePost_Test_Count_Should_Increment()
        {
            //Arrange
            var       countBefore = UsingDbContext(a => a.Post.Count());
            PostInput postDto     = new PostInput()
            {
                AuthorId = UsingDbContext <User>(a => a.User.FirstOrDefault()).Id,
                Content  = "HI",
                Title    = "title5"
            };

            //Act
            var flag = await _postAppService.CreatePost(postDto);

            var count = UsingDbContext(a => a.Post.Count());

            //Assert
            Assert.NotNull(flag);
            Assert.Equal(countBefore + 1, count);

            this.Dispose();
        }