public void TestMethod1() { Mapper.Initialize(cfg => { cfg.CreateMap <Picture, PictureDTO>() .ForMember(dest => dest.GaleryName, opt => opt.MapFrom(src => src.Galery.Name)) .ForMember(dest => dest.GaleryId, opt => opt.MapFrom(src => src.Galery.Id)); }); var mockRepository = new Mock <IPictureRepository>(); mockRepository.Setup(x => x.GetById(4)).Returns(new Picture() { Id = 4 }); var controller = new PicturesController(mockRepository.Object); IHttpActionResult actionResult = controller.GetById(4); var contentResult = actionResult as OkNegotiatedContentResult <PictureDTO>; Assert.IsNotNull(contentResult); Assert.IsNotNull(contentResult.Content); Assert.AreEqual(4, contentResult.Content.Id); }
public void GetReturnsProductWithSameId() { Mapper.Initialize(cfg => { cfg.CreateMap <Picture, PictureDTO>() // automatski će mapirati Author.Name u AuthorName .ForMember(dest => dest.GalleryName, opt => opt.MapFrom(src => src.Galery.Name)); // ako želimo eksplicitno zadati mapiranje }); // Arrange var mockRepository = new Mock <IPictureRepo>(); mockRepository.Setup(x => x.GetById(42)).Returns(new Picture { Id = 42 }); var controller = new PicturesController(mockRepository.Object); // Act IHttpActionResult actionResult = controller.GetById(42); var contentResult = actionResult as OkNegotiatedContentResult <PictureDTO>; // Assert Assert.IsNotNull(contentResult); Assert.IsNotNull(contentResult.Content); Assert.AreEqual(42, contentResult.Content.Id); }