Rob*_*ert 5 c# model-view-controller action
我有Action如下:
public PartialViewResult MyActionIWantToTest(string someParameter)
{
// ... A bunch of logic
return PartialView("ViewName", viewModel);
}
Run Code Online (Sandbox Code Playgroud)
当我检查结果时,它有一些属性,但它们是null或空.唯一具有任何内容的属性是ViewEngineCollection不包含任何特定于我的方法的内容.
有没有人有一些测试的示例代码PartialViewResult?
假设你有一个Action看起来像这样的东西:
public PartialViewResult MyActionIWantToTest(string someParameter)
{
var viewModel = new MyPartialViewModel { SomeValue = someParameter };
return PartialView("MyPartialView", viewModel);
}
Run Code Online (Sandbox Code Playgroud)
注意:MyPartialViewModel是一个只有一个属性的简单类 - SomeValue.
NUnit示例可能如下所示:
[Test]
public void MyActionIWantToTestReturnsPartialViewResult()
{
// Arrange
const string myTestValue = "Some value";
var ctrl = new StringController();
// Act
var result = ctrl.MyActionIWantToTest(myTestValue);
// Assert
Assert.AreEqual("MyPartialView", result.ViewName);
Assert.IsInstanceOf<MyPartialViewModel>(result.ViewData.Model);
Assert.AreEqual(myTestValue, ((MyPartialViewModel)result.ViewData.Model).SomeValue);
}
Run Code Online (Sandbox Code Playgroud)