public void Should_Edit_Present_Book() { Book book1 = controller.Edit(1).ViewData.Model as Book; Book book2 = controller.Edit(2).ViewData.Model as Book; Book book3 = controller.Edit(3).ViewData.Model as Book; Assert.AreEqual(1, book1.BookId); Assert.AreEqual(2, book2.BookId); Assert.AreEqual(3, book3.BookId); }
public void UploadImage(string uploadFiles) { //Setup a fake HttpRequest Mock <HttpContextBase> moqContext = new Mock <HttpContextBase>(); Mock <HttpRequestBase> moqRequest = new Mock <HttpRequestBase>(); Mock <HttpFileCollectionBase> moqPostedFileCollection = new Mock <HttpFileCollectionBase>(); Mock <HttpServerUtilityBase> moqServer = new Mock <HttpServerUtilityBase>(); string imagePath = Path.Combine(ProjectLocation.FromFolder("BookShop").FullPath, @"App_Data\Images"); moqServer.Setup(s => s.MapPath("~/App_Data/Images/")).Returns(imagePath); moqContext.Setup(x => x.Server).Returns(moqServer.Object); moqContext.Setup(x => x.Request).Returns(moqRequest.Object); IEnumerable <string> fileNames = from t in uploadFiles.Split(',') select t.Trim().Trim('\''); moqPostedFileCollection.Setup(c => c.Count).Returns(fileNames.Count()); /// moqRequest.Setup(r => r.Files).Returns(moqPostedFileCollection.Object); for (int i = 0; i < fileNames.Count(); i++) { Mock <HttpPostedFileBase> moqPostedFile = new Mock <HttpPostedFileBase>(); moqPostedFile.Setup(p => p.SaveAs(It.IsAny <string>())).Verifiable(); moqPostedFileCollection.Setup(c => c[i]).Returns(moqPostedFile.Object); /// } //Pass the fake current HttpContext into the ControllerContext of CatalogController _controller.ControllerContext = new ControllerContext(moqContext.Object, new RouteData(), _controller); Book book = ScenarioContext.Current.Get <Book>("book"); //Get the action from ScenarioContext object so that we can call the suitable action in CatalogController (Create/Edit Book) if (ScenarioContext.Current.Get <string>("isCreated") == "Y") { _result = _controller.Create(book); } else { _result = _controller.Edit(book); } }