title: Unit Testing MVC 4 RedirectToAction
body: <p>I am trying to unit test the redirection of my controller in MVC 4 .Net 4.5. Here is an example:</p>
  
  <pre class="lang-cs prettyprint-override"><code>    [TestMethod]
      public void Register_PassValidModel_RedirectToHomeIndexShouldBeTrue()
      {
          //Arrange
          var registerModel = new RegisterModel
          {
              Email = "validEmailAddress@domain.com",
              Password = "password"
          };
  
          //Assign
          var result = _controller.Register(registerModel) as RedirectToRouteResult;
  
          //Assert
          result.RouteValues["Action"].ShouldBeEqual("Index");
          result.RouteValues["Controller"].ShouldBeEqual("Home");
      }
  </code></pre>
  
  <p>Here is the controller: </p>
  
  <pre class="lang-cs prettyprint-override"><code>    [HttpPost]
      public ActionResult Register(RegisterModel model)
      {
           if (ModelState.IsValid)
          {
              var userToRegister = new User { Email = model.Email, Password = model.Password };
              var service = new UserService(_userRepository);
  
              User user = service.RegisterUser(userToRegister);
  
              if (user.UserErrorMessages.Count != 0)
              {
                  user.UserErrorMessages.ForEach(x =&gt; ModelState.AddModelError("", x));
                  return View(model);
              }
  
              SetCookie(model.Email);
  
              return RedirectToAction("Index", "Home");
          }
          return View(model);
      }
  </code></pre>
  
  <p>The issue the variable <code>result</code> in the Unit Test is <code>null</code>. I found this code from someone who was working on a MVC 2 project and it seemed to work for him. Has something changed with MVC 4?</p>
  
  <p>Thanks in advance!</p>