运行单元测试时出现此错误:
When assigning attributes, you must pass a hash as an argument.
,并且我也收到此警告:
Error Message:
System.ArgumentNullException : Value cannot be null.
Parameter name: logger
是否可以使记录器静音或对其进行测试?
仍然是编程新手,渴望学习。已经为此苦苦挣扎了很长时间。感谢您的帮助和指导。
使用堆栈跟踪更新
Field 'OrderControllerTest._logger' is never assigned to, and will always have its default value null
到目前为止我所拥有的:
OrderControllerTest.cs:
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
OrderController.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);
}
}
}
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;
}
}
}
}
OrderRepository.cs:
ILogger<OrderController> _logger;
public OrderControllerTest()
{
_logger = new Mock<ILogger<OrderController>>().Object;
}
答案 0 :(得分:2)
假设您已从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>()));
}
现在您应该使用以下命令初始化控制器:
var controller = new OrderController(_logger.Object);
答案 1 :(得分:1)
在“ OrderControllerTest”类的构造函数中,您需要创建ILogger接口的模拟。 Logger是OrderController的依赖项,您不想在控制器的单元测试中测试其功能。
有很多好的模拟框架。我更喜欢Moq,但还有很多其他人。然后,您需要模拟OrderController使用的方法。
Moq示例:
using ...
using Moq;
namespace Tests
{
[TestFixture]
public class OrderControllerTest
{
public OrderControllerTest()
{
var mockLogger = new Mock<ILogger<OrderController>>();
mockLogger.Setup( x => x.LogError( It.IsAny<Exception>(), It.IsAny<string>() );
_logger = mockLogger.Object;
}
...
}
}