示例#1
0
 public ActionResult AddHobby(HobbyModel model)
 {
     if (ModelState.IsValid)
     {
         _uow.HobbiesRepository.Insert(model.ToDto(_uow.LanguagesRepository.Get().ToList()));
         _uow.Save();
         return(RedirectToAction(nameof(ShowHobbies)));
     }
     return(RedirectToAction(nameof(AddHobby)));
 }
        public void Should_Transform_HobbyModel_To_Hobby()
        {
            var image = TestResources.TestsResources.test;

            var expectedStream = new MemoryStream();

            image.Save(expectedStream, ImageFormat.Jpeg);

            var expectedWebImage = new WebImage(expectedStream);

            expectedWebImage.Resize(350, 200, false);

            var expected = new Hobby()
            {
                Id      = 1,
                Content = expectedWebImage.GetBytes(),
                Texts   = new List <Text>()
                {
                    new Text()
                    {
                        Language = _languages[0], Value = "hb1.fr"
                    },
                    new Text()
                    {
                        Language = _languages[1], Value = "hb1.en"
                    }
                }
            };

            var imgFake = new Mock <HttpPostedFileBase>();

            imgFake.Setup(x => x.InputStream).Returns(new MemoryStream(expectedWebImage.GetBytes()));

            var act = new HobbyModel()
            {
                Picture = imgFake.Object,
                Texts   = new List <TextModel>()
                {
                    new TextModel()
                    {
                        Language = _languages[0], Value = "hb1.fr"
                    },
                    new TextModel()
                    {
                        Language = _languages[1], Value = "hb1.en"
                    }
                }
            };

            var result = act.ToDto(_languages);

            Assert.AreEqual(expected.Content, result.Content);
            AssertExtension.CompareIEnumerable(expected.Texts, result.Texts,
                                               (x, y) => x.Language == y.Language && x.Value == y.Value);
        }