예제 #1
0
        public void AddComment_IfPhotoIsNull_ShouldReturnNotFoundResult()
        {
            var mockService     = new Mock <IPhotoService>();
            var fakeUserManager = new FakeUserManagerBuilder()
                                  .Build();
            var user = new ClaimsPrincipal(new ClaimsIdentity(new Claim[]
            {
                new Claim(ClaimTypes.Name, "test1"),
                new Claim(ClaimTypes.NameIdentifier, "1"),
                new Claim("custom-claim", "example claim value"),
            }, "mock"));

            fakeUserManager.Setup(u => u.FindByNameAsync("test1")).Returns(Task.FromResult(new User  {
                Id = "1", UserName = "******"
            }));
            var controller = new PhotoController(mockService.Object, _mapper, fakeUserManager.Object);
            var context    = new ControllerContext
            {
                HttpContext = new DefaultHttpContext
                {
                    User = user
                }
            };

            controller.ControllerContext = context;

            var result = controller.AddComment(1, new Comment {
                Id = 1
            });

            Assert.IsInstanceOf <NotFoundResult>(result);
        }
예제 #2
0
        public void AddComment_ShouldCallAddCommentAndReturnOkResult()
        {
            int photoId     = 1;
            var mockService = new Mock <IPhotoService>();

            mockService.Setup(service => service.GetPhotoById(photoId)).Returns(Photos[photoId]);
            var fakeUserManager = new FakeUserManagerBuilder()
                                  .Build();
            var user = new ClaimsPrincipal(new ClaimsIdentity(new Claim[]
            {
                new Claim(ClaimTypes.Name, "test1"),
                new Claim(ClaimTypes.NameIdentifier, "1"),
                new Claim("custom-claim", "example claim value"),
            }, "mock"));

            fakeUserManager.Setup(u => u.FindByNameAsync("test1")).Returns(Task.FromResult(new User  {
                Id = "1", UserName = "******"
            }));
            var controller = new PhotoController(mockService.Object, _mapper, fakeUserManager.Object);
            var context    = new ControllerContext
            {
                HttpContext = new DefaultHttpContext
                {
                    User = user
                }
            };

            controller.ControllerContext = context;


            var result = controller.AddComment(photoId, new Comment {
                Id = 1
            });


            mockService.Verify(service => service.AddComment(It.IsAny <Photo>(), It.IsAny <Comment>()), Times.Once());
            Assert.IsInstanceOf <OkResult>(result);
        }