[英]How do I unit test an action that returns a ToList
Hello I'm trying to run some tests for this Controller Method but I'm struggling to test the View, here's my controller class.您好,我正在尝试为此控制器方法运行一些测试,但我正在努力测试视图,这是我的控制器类。
public class StockController : Controller
{
private readonly IStockService _stockService;
public StockController(IStockService stockService)
{
_stockService = stockService;
}
// GET: Stock
public async Task<IActionResult> Index()
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
IEnumerable<StockDto> stocks = null;
try
{
stocks = await _stockService.GetStockAsync();
}
catch (HttpRequestException)
{
stocks = Array.Empty<StockDto>();
}
return View(stocks.ToList());
}
}
Here is the Test Method on the MS Test Project for the Stock Controller, I've got the Arrange and Act working fine, but for the Asserts there is a red line on the 'ViewResult' and I'm not sure why.这是股票控制器的 MS 测试项目的测试方法,我的安排和行为工作正常,但对于断言,“ViewResult”上有一条红线,我不知道为什么。
[TestMethod]
public async Task ReturnIndexMethod()
{
//Arrange
var controller = new StockController(new FakeStockService());
//Act
var result = await controller.Index();
//Assert
Assert.IsNotNull(result);
var AResult = result as ViewResult;
Assert.IsNotNull(AResult);
var stockResult = AResult.Value as IEnumerable<StockDto>;
Assert.IsNotNull(stockResult);
}
Also here is the FakeStockService
that will supply mock test Data just in case people need to see it.这里还有FakeStockService
,它将提供模拟测试数据,以防人们需要查看它。
public class FakeStockService : IStockService
{
private readonly IEnumerable<StockDto> _stock = new List<StockDto>
{
new StockDto { Id = 1, Name = "asfdaf", Description = "afsdfsfdsda", Price = 5000 }
};
public Task<IEnumerable<StockDto>> GetStockAsync()
{
return Task.FromResult(_stock);
}
}
Any help or suggestions would be appreciated, I've just come at a loss here after trying everything else.任何帮助或建议将不胜感激,在尝试了其他一切之后,我在这里不知所措。
Maybe the test is missing the namespace in order to access that ViewResult
type.也许测试缺少命名空间以访问该ViewResult
类型。
using Microsoft.AspNetCore.Mvc;
//...
Also, unless that is a typo, the view result should have a Model
property, not Value
like you have there.此外,除非这是一个错字,否则视图结果应该有一个Model
属性,而不是像你那样的Value
。
[TestMethod]
public async Task ReturnIndexMethod()
{
//Arrange
var controller = new StockController(new FakeStockService());
//Act
var result = await controller.Index();
//Assert
Assert.IsNotNull(result);
var view = result as ViewResult;
Assert.IsNotNull(view);
var stockResult = view.Model as IEnumerable<StockDto>;
Assert.IsNotNull(stockResult);
}
Everything else in the test looks fine to me.测试中的其他所有内容对我来说都很好。
The controller action however appears to be accessing model state on a GET request.然而,控制器操作似乎是在 GET 请求上访问模型状态。 The current example shown does not any obvious need to use the model state so I would suggest removing that显示的当前示例没有任何明显需要使用模型状态,因此我建议删除它
public async Task<IActionResult> Index() {
IEnumerable<StockDto> stocks = null;
try {
stocks = await _stockService.GetStockAsync();
}
catch (HttpRequestException) {
stocks = Array.Empty<StockDto>();
}
return View(stocks.ToList());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.