How to get the Model from an ActionResult?

前端 未结 4 954
既然无缘
既然无缘 2020-12-30 23:25

I\'m writing a unit test and I call an action method like this

var result = controller.Action(123);

result is ActionResult and

4条回答
  •  南方客
    南方客 (楼主)
    2020-12-30 23:44

    It's somewhat cheating but a very trivial way to do so in .NET4

    dynamic result = controller.Action(123);
    
    result.Model
    

    Used this today in a unit test. Might be worth some sanity checks such as:

    Assert.IsType(result); 
    Assert.IsType(result.Model);
    
    Assert.Equal(123, result.Model.Id);
    

    You could skip the first one if the result is going to be a view or partial result depending on the input.

提交回复
热议问题