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 ?!
 }
有帮助吗?

解决方案

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"]);
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top