我刚刚开始一个新项目,并且该解决方案没有单个单元测试。所以我认为提高我在单元测试中低于平均水平的技能并学习应用程序的一个好方法就是在我进行测试时编写测试。Xunit设置方法不能按预期工作
我在xunit之前没有做过太多的工作,所以这可能是一个简单的问题,我已经自己寻找答案,但似乎找不到答案。
它是我试图测试的webapi项目。该控制器有一个基本控制器,其中包含其记录日志的方法。
public class BaseController : ApiController
{
private readonly ILog _log;
public BaseController(ILog log)
{
_log = log;
}
[ApiExplorerSettings(IgnoreApi = true)]
public virtual void Log(Enums.RequestType requestType, string detail)
{
_log.Info(string.Format("{0} | {1}",requestType, detail));
}
从我的控制器
public virtual async Task<ActionResult<MyDetail>> Get([FromUri]long id)
{
Enums.RequestType requestType = Enums.RequestType.GET;
Log(requestType, "start called");
var card = await _repository.FindItemAsync(id);
Log(requestType, "thats a rap");
}
我的测试,到目前为止,看起来像
[Fact()]
public void TestMyDetailsReturnsDetailAndLogs()
{
var detail = MockDetail();
_myController = new Mock<MyDetailController>(_repository.Object, _logging.Object);
_myController.Setup(m => m.Log(It.IsAny<Enums.RequestType>(),
It.IsAny<string>()));
_repository.Setup(x => x.FindItemAsync(It.IsAny<long>())
.ReturnsAsync(detail);
var controllerResult = _myController.Object.Get(detail.Id);
_myController.VerifyAll();
var negotiatedResult = controllerResult.Result;
Assert.NotNull(negotiatedResult);
附加信息:以下设置不匹配:
MyController m => m.Log(It.IsAny<RequestType>(), It.IsAny<String>())
2个问题,为什么我会收到此消息?因为我已经安装了m.Log
我的控制器和存储库中的断点是否应该被击中?
任何帮助将是伟大的!