【发布时间】:2018-03-10 07:09:33
【问题描述】:
虽然我的控制器功能正常,但我的 单元测试 在 .Name 的“断言”行中遇到语法错误
IAction 结果不包含“名称”的定义...
如果我在调试模式下将鼠标悬停在“结果”上,我可以看到数据在结果变量中(结果 > 模型 > 名称)。我尝试使用 Result.Model.Name 访问它,但这也是语法错误。
单元测试:
[Fact]
public async Task TestGetNameById()
{
string expectedName = "Component";
using (var context = GetContextWithData())
using (var controller = new AssetTypesController(context))
{
var result = await controller.Details(2);
Assert.Equal(expectedName, result.Name);
}
}
控制器动作:
public async Task<IActionResult> Details(int? id)
{
if (id == null)
{
return NotFound();
}
var assetType = await _context.AssetType
.SingleOrDefaultAsync(m => m.AssetTypeId == id);
if (assetType == null)
{
return NotFound();
}
return View(assetType);
}
型号
public class AssetType
{
[DatabaseGenerated(databaseGeneratedOption: DatabaseGeneratedOption.Identity)]
[Key]
public int AssetTypeId { get; set; }
[Required]
public string Name { get; set; }
}
【问题讨论】:
标签: c# asp.net-mvc asp.net-core async-await xunit