简体   繁体   English

如何对返回 ToList 的操作进行单元测试

[英]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.

相关问题 如何在返回IHttpActionResult时对web api动作方法进行单元测试? - How do I unit test web api action method when it returns IHttpActionResult? 我如何单元测试返回PartialViewResult的MVC Action? - How can I unit test an MVC Action that returns a PartialViewResult? 如何对返回Action的方法进行单元测试 <AuthenticationOptions> ? - How to unit test a method that returns Action<AuthenticationOptions>? 如何对传递给其他方法的操作进行单元测试 - How do I unit test an Action Passed to another method 如何对调用Action的HTML Helper进行单元测试? - How do I unit test a HTML Helper that does a call to an Action? 如何对返回无效值的命令处理程序进行单元测试? - How do I unit test a Command Handler that returns a void? 如何对返回IEnumerable的方法进行单元测试 - How do I Unit Test a method that returns IEnumerable 如何使用Moq对该动作进行单元测试? - How can I unit test this action with Moq? 如何单元测试一个返回视图的GlassController动作模型 - How to Unit Test a GlassController Action which Returns a View Taking a Model 如何对返回 JsonResult 的 Action 方法进行单元测试? - How to unit test an Action method which returns JsonResult?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM