我目前正在使用以下代码在我的单元测试控制器的构造器中模拟 ILogger:
private readonly Mock<ILogger> _logger = new Mock<ILogger>();
我需要使用这个 Mock ILogger 来记录在各种单元测试中被断言的抛出异常。
例如:
[Test]
public void Arguments_CallBoardStatsRepo_Null()
{
Assert.Throws<NullReferenceException>(() => new AgentsController(null, _clientCallsRepoMock.Object, _agentStatusRepoMock.Object, _logger.Object));
_logger.Verify(m => m.Error("Error", It.IsAny<NullReferenceException>()), Times.Once);
}
我需要为模拟的记录器(_logger)添加 ArgumentNullException 检查。
实现这一目标的最佳方法是什么?
编辑:正在测试的控制器
public class AgentsController : ApiController
{
readonly IAgentStatusRepo _agentStatusRepo;
readonly ICallBoardStatsRepo _callBoardRepo;
readonly IClientCallsRepo _clientCallRepo;
readonly ILogger _logger;
public AgentsController(ICallBoardStatsRepo callBoardRepo,
IClientCallsRepo clientCallRepo,
IAgentStatusRepo agentStatusRepo,
ILogger logger)
{
Util.Guard.ArgumentsAreNotNull(callBoardRepo, clientCallRepo, agentStatusRepo);
_callBoardRepo = callBoardRepo;
_clientCallRepo = clientCallRepo;
_agentStatusRepo = agentStatusRepo;
_logger = logger;
}
[HttpGet]
[Route("api/agents")]
public IHttpActionResult FindAllAgentsByClientGroup(string group)
{
IEnumerable<AgentStatus> agentCallStats = _agentStatusRepo.ByGroupKey(group).ToList();
return Ok(agentCallStats);
}
}