问题描述
我在尝试针对asp.net核心管道的组件调试xUnit时看到奇怪的行为.下面发布的代码删除了所有有目的的功能,仅用于说明问题:
I'm seeing odd behavior while attempting to debug xUnits against components of the asp.net core pipeline. The code posted below has all purposeful functionality stripped out to illustrate the problem only which is :
- 没有在JsonModelBinder中达到我的所有断点.
- 即使正在评估,也不会退出返回Task.Completed".
JsonModelBinder的生产代码包含更多逻辑来反序列化输入的字符串数据.此代码包含失败逻辑,其中包含许多返回Task.Completed语句.使用此代码时,调试器将评估这些return语句,但仍会继续执行,直到到达方法末尾,一直到末尾才返回.
The production code for JsonModelBinder contains more logic to deserialize the incoming string data. This code contains failure logic which contains a number of return Task.Completed statements. When using this code the the debugger will evaluate these return statements but continue onward, not returning until reaching the end of the method, always reaching the end.
我正在使用Moq,xUnit,VS2017,ASP.net Core 2.2.
I'm using Moq, xUnit, VS2017, ASP.net Core 2.2.
//简单事实
[Fact]
public async Task BindModelAsync_WithNullValueProvider_SetsDefaultError()
{
// arrange
var queryStringCollection = new RouteValueDictionary
{
{"Page", "1"},
{"Size", "20"}
};
var valueProvider = new RouteValueProvider(BindingSource.Path, queryStringCollection);
ModelBindingContext bindingContext = new DefaultModelBindingContext
{
ModelName = "Test",
ValueProvider = valueProvider
};
var jsonBinder = new JsonModelBinder();
// act
await jsonBinder.BindModelAsync(bindingContext);
// not point in asserting :-)
}
//JsonModelBinder
// JsonModelBinder
public class JsonModelBinder : IModelBinder
{
private readonly IOptions<MvcJsonOptions> _jsonOptions;
private readonly ILoggerFactory _loggerFactory;
public JsonModelBinder() { }
public Task BindModelAsync(ModelBindingContext bindCtx)
{
string modelName = bindCtx.ModelName;
Debug.Print(modelName);
if (string.IsNullOrEmpty(modelName))
{
return Task.CompletedTask;
}
return Task.CompletedTask;
}
}
** 编辑用于项目参考
推荐答案
我的一位同事遇到了同样的问题.经过大量调试和调查,我们发现这为他解决了问题.
One of my colleagues encountered the same problem.After a lot of debugging and investigation, we found this fixed the problem for him.
- 右键单击Visual Studio中的解决方案,然后执行清理解决方案".
- 手动删除项目的obj和bin文件夹中的内容.
- 删除解决方案根目录中.vs文件夹的内容. (如果文件已锁定,请关闭Visual Studio.)
最后一步似乎很重要.
这篇关于Asp.net core 2.2 ModelBinder单元测试问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!