コード例 #1
0
        public void PictureController_Update()
        {
            //Arrange
            var pictureRepo = new FakeRepository <Picture>(p => p.PictureID);
            var tagsRepo    = new FakeRepository <Tag>(e => e.TagID);

            var postedfilesKeyCollection = new Mock <HttpFileCollectionBase>();
            var fakeFileKeys             = new List <string>()
            {
                "file"
            };
            var postedfile = new Mock <HttpPostedFileBase>();

            Request.Setup(req => req.Files).Returns(postedfilesKeyCollection.Object);
            postedfilesKeyCollection.Setup(keys => keys.GetEnumerator()).Returns(fakeFileKeys.GetEnumerator());
            postedfilesKeyCollection.Setup(keys => keys["file"]).Returns(postedfile.Object);
            postedfile.Setup(e => e.InputStream).Returns(new MemoryStream(TEST_BMP));
            postedfile.Setup(e => e.FileName).Returns("filename");
            postedfile.Setup(e => e.ContentLength).Returns(TEST_BMP.Length);
            postedfile.Verify(f => f.SaveAs(It.IsAny <string>()), Times.AtMostOnce());

            //Act
            var controller = new PicturesController(pictureRepo, tagsRepo);

            controller.ControllerContext = new ControllerContext(Context.Object, new RouteData(), controller);
            var createResult = controller.Create(postedfile.Object, "tag1, tag2, tag3", "custom name") as RedirectToRouteResult;
            var inserted     = pictureRepo.GetAll().FirstOrDefault();
            var updateResult = controller.Edit((PictureView)inserted, "tag1, tag2") as RedirectToRouteResult;

            //Assert
            Assert.IsNotNull(createResult);                                                                      // Check if the view returned a valid result
            Assert.IsNotNull(inserted);                                                                          // Check if the picture was inserted
            Assert.IsNotNull(updateResult);                                                                      // Check if the view returned a valid result.
            Assert.IsTrue(new string[] { "tag1", "tag2" }.SequenceEqual(inserted.Tags.Select(t => t.TagLabel))); // Check if the tags were set correctly
        }