【发布时间】:2018-01-09 16:32:11
【问题描述】:
您好,我正在为我的控制器中的以下方法编写单元测试用例。
[HttpGet]
[Route("GetList")]
public IHttpActionResult GetAllMasterList()
{
var MasterList = _masterListRepo.GetAll().Select(t => new { t.MASTER_ID, t.MASTER_NAME }).OrderBy(t=>t.MASTER_NAME).ToList();
return Ok(MasterList);
}
对于上述方法,单元测试用例方法如下。
[TestMethod]
public void AllMastersList()
{
//Arrange
var controller = new MastersController();
var actualResults = _masterListRepo.GetAll().ToList();
//Act
var actionResult = controller.GetAllMasterList();
//Assert
Assert.IsTrue(actionResult.GetType().GetGenericTypeDefinition() == typeof(OkNegotiatedContentResult<>));
var contentExpected = actionResult as OkNegotiatedContentResult<IEnumerable<dynamic>>;
Assert.IsNotNull(contentExpected.Content.ToList());
Assert.AreEqual(contentExpected.Content.Count(), actualResults.Count);
}
我得到contentExpected 为null。如何转换此 Ok() 结果以获取值。怎么做?
【问题讨论】:
-
看看我这里给的答案stackoverflow.com/a/38446754/5233410
标签: c# unit-testing asp.net-web-api2 mstest