public ActionResult New(NewReviewViewModel model)
 {
     if (ModelState.IsValid)
     {
         IEnumerable<FileChange> changes = null;
         try
         {
             using (StreamReader rdr = new StreamReader(model.Diff.InputStream))
             {
                 changes = Diff.CreateFromGitDiff(rdr);
             }
         }
         catch (Exception ex)
         {
             ModelState.AddModelError("Diff", "An error occurred reading the diff: " + ex.Message);
         }
         if (changes != null)
         {
             // Create the review
             Review created = Reviews.CreateReview(model.Name, changes.ToList(), Auth.GetCurrentUserId());
             return RedirectToAction("View", "Reviews", new { id = created.Id });
         }
     }
     return View(model);
 }
            public void ReturnsErrorIfDiffServiceThrows()
            {
                // Arrange
                var ctl = CreateController();
                var model = new NewReviewViewModel() { Name = "123", Diff = new TestPostedFile("Test") };
                ctl.MockDiff
                   .Setup(s => s.CreateFromGitDiff(It.Is<StreamReader>(r => ReferenceEquals(r.BaseStream, model.Diff.InputStream))))
                   .Throws(new NotSupportedException("Borked!"));

                // Act
                var result = ctl.New(model);

                // Assert
                ActionAssert.IsViewResult(result, model);
                Assert.Contains("An error occurred reading the diff: Borked!", ctl.ModelState["Diff"].Errors.Select(e => e.ErrorMessage).ToArray());
            }
            public void ReturnsViewIfModelInvalid()
            {
                // Arrange
                var ctl = CreateController();
                var model = new NewReviewViewModel() { Name = "123" };
                ctl.ModelState.AddModelError("", "Test");

                // Act
                var result = ctl.New(model);

                // Assert
                ActionAssert.IsViewResult(result, model);
            }
            public void CreatesReviewWithCurrentUserAsOwnerFromDiff()
            {
                // Arrange
                var userId = 42;
                var ctl = CreateController();
                var model = new NewReviewViewModel() { Name = "123", Diff = new TestPostedFile("Test") };
                var changes = new FileChange[] { new FileAddition(), new FileDeletion(), new FileModification() };
                ctl.MockDiff
                   .Setup(s => s.CreateFromGitDiff(It.Is<StreamReader>(r => ReferenceEquals(r.BaseStream, model.Diff.InputStream))))
                   .Returns(changes);
                ctl.MockAuth
                   .Setup(s => s.GetCurrentUserId())
                   .Returns(userId);

                // Act
                var result = ctl.New(model);

                // Assert
                ActionAssert.IsRedirectResult(result, new { controller = "Reviews", action = "View", id = ctl.TestData.LastId });
                Assert.Contains(new Review() {
                    Id = ctl.TestData.LastId,
                    UserId = 42,
                    Name = "123",
                    Files = changes.ToList()
                }, ctl.Reviews.Data.Reviews, new PropertyEqualityComparer());
            }