Question

How to getting TempData after RedirectToAction in Rhino Mock test?

Code

public ActionResult Action1() {
    TempData["Foo"] = "Bar";
    return RedirectToAction("Action2");
}
public ActionResult Action2() {
    return View();
}

Test

 using (var controller = new TestController(x => x.Register(service))){
    var result = (RedirectToRouteResult)controller.Action1();

    // How to get TempData value there ?!
 }
Was it helpful?

Solution

Properties and methods of base classes are part of your derived class, and are therefore part of the system under test (SUT). You only need to mock dependencies of the system under test - other classes that the system under test interacts with.

In this situation, you don't need a mock, because TempData is a property of ControllerBase, which your controller is derived from:

using (var controller = new TestController(x => x.Register(service)))
{
    var result = (RedirectToRouteResult)controller.Action1();
    Assert.AreEqual("Bar", controller.TempData["Foo"]);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top