【发布时间】:2018-06-24 03:35:43
【问题描述】:
我正在使用 C# 和 .NET Core 2.0 开发 ASP.NET Core 2 Web api。
我更改了一种方法,将其添加到 try-catch 以允许我返回状态代码。
public IEnumerable<GS1AIPresentation> Get()
{
return _context
.GS1AI
.Select(g => _mapper.CreatePresentation(g))
.ToList();
}
改为:
public IActionResult Get()
{
try
{
return Ok(_context
.GS1AI
.Select(g => _mapper.CreatePresentation(g))
.ToList());
}
catch (Exception)
{
return StatusCode(500);
}
}
但现在我的 Test 方法有问题,因为现在它返回 IActionResult 而不是 IEnumerable<GS1AIPresentation>:
[Test]
public void ShouldReturnGS1Available()
{
// Arrange
MockGS1(mockContext, gs1Data);
GS1AIController controller =
new GS1AIController(mockContext.Object, mockMapper.Object);
// Act
IEnumerable<Models.GS1AIPresentation> presentations = controller.Get();
// Arrange
Assert.AreEqual(presentations.Select(g => g.Id).Intersect(gs1Data.Select(d => d.Id)).Count(),
presentations.Count());
}
我的问题在这里:IEnumerable<Models.GS1AIPresentation> presentations = controller.Get();。
我是否需要重构并创建一个新方法来测试Select?
这个选择:
return _context
.GS1AI
.Select(g => _mapper.CreatePresentation(g))
.ToList();
或者我可以在IActionResult 中获得IEnumerable<Models.GS1AIPresentation>
【问题讨论】:
标签: c# unit-testing asp.net-core nunit