gpt4 book ai didi

c# - 系统.ArgumentNullException : Value cannot be null in unit test - C#

转载 作者:太空宇宙 更新时间:2023-11-03 22:42:34 24 4
gpt4 key购买 nike

我在运行单元测试时遇到此错误:错误信息:
System.ArgumentNullException:值不能为空。
Parameter name: logger
而且我也收到这个警告:字段“OrderControllerTest._logger”从未分配给,并且始终具有默认值 null

有没有办法让记录器静音或测试它?

对编程还是个新手,渴望学习。已经为此苦苦挣扎了很长时间。感谢您的帮助和指导。

使用堆栈跟踪更新

Stack Trace:
at Microsoft.Extensions.Logging.LoggerExtensions.Log(ILogger logger, LogLevel logLevel, EventId eventId, Exception exception, String message, Object[] args)
at Microsoft.Extensions.Logging.LoggerExtensions.LogError(ILogger logger, Exception exception, String message, Object[] args)
at Project.Controllers.OrderController.CustomerOrders(String customerId) in C:\projects\Project\Controllers\OrderController.cs:line 34
at Tests.OrderControllerTest.AllOrdersForCustomer() in C:\projects\UnitTests\Project.Service.Tests\OrderControllerTest.cs:line 74

我目前拥有的:

OrderControllerTest.cs:

using System;
using System.Collections.Generic;
using NUnit.Framework;
using Project.Models;
using Project.Controllers;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace Tests
{
[TestFixture]
public class OrderControllerTest
{
ILogger<OrderController> _logger;

public OrderControllerTest()
{

}

[Test]
public void AllOrdersForCustomer()
{
// Arrange
var controller = new OrderController(_logger);
var expectedResult = new List<Order>();
var oneOrder = new Order()
{
orderId = 228,
product= "Jeans",
status = "Ready for shipping",
price= 20$
};

var twoOrder = new Order()
{
caseId = 512,
creditorName = "Basketball",
status = "Shipped",
totalOpenBalance = 30$
};
expectedResult.Add(oneOrder);
expectedResult.Add(twoOrder);

// Act
var actionResult = controller.CustomerOrders("1243");
var result = (List<Order>)actionResult;

// Assert
for (var i = 0; i < result.Count; i++)
{
CollectionAssert.AreEqual(expectedResult, result);
}
}
}

OrderController.cs:

using System;
using System.Collections.Generic;
using Project.Models;
using Project.Repositories;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;


namespace Project.Controllers
{
public class OrderController : Controller
{
ILogger<OrderController> _logger;

public OrderController(ILogger<OrderController> logger)
{
_logger = logger;
}

[HttpGet("customers/{customerId}/orders")]
public List<Order> CustomerOrders(string customerId)
{
try
{
return OrderRepository.AllOrders(customerId);
}
catch (Exception ex)
{
_logger.LogError(ex, "Cant retrieve no orders.");
return null;
}
}
}
}

 ILogger<OrderController> _logger;

public OrderControllerTest()
{
_logger = new Mock<ILogger<OrderController>>().Object;
}

OrderRepository.cs:

    public static List<Order> AllOrders(string customerId)
{
var orderlist = new List<Order>();


  if (Order!=null && Order.Count>0)
{
if (Order.TryGetValue(customerId, out orderlist))
return orderlist;
}
return FromDB(customerId);

}

最佳答案

假设您已从 EL MOJO 获取输入,您需要在测试中使用 Moq 初始化 ILogger:

Mock<ILogger<OrderController>> _logger;

public OrderControllerTest()
{
_logger = new Mock<ILogger<OrderController>>();
_logger.Setup(m => m.LogError(It.IsAny<Exception>(), It.IsAny<string>()));
}

现在您应该使用以下内容初始化您的 Controller :

var controller  = new OrderController(_logger.Object);

关于c# - 系统.ArgumentNullException : Value cannot be null in unit test - C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51526607/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com