コード例 #1
0
        public void PictureController_Delete()
        {
            //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 deleteResult  = controller.DeleteConfirmed(inserted.PictureID) as RedirectToRouteResult;
            var insertedCount = pictureRepo.GetAll().Count();

            //Assert
            Assert.IsNotNull(createResult);    // Check if the view returned a valid result
            Assert.IsNotNull(inserted);        // Check if the picture was inserted
            Assert.IsNotNull(deleteResult);    // Check if the view returned a valid result.
            Assert.IsTrue(insertedCount == 0); // Checks if the picture was deleted.
        }