为了账号安全,请及时绑定邮箱和手机立即绑定

Asp.net core 2.2 ModelBinder 单元测试问题

Asp.net core 2.2 ModelBinder 单元测试问题

C#
隔江千里 2022-10-15 14:22:24
在尝试针对 asp.net 核心管道的组件调试 xUnit 时,我看到了奇怪的行为。下面发布的代码删除了所有有目的的功能,仅用于说明问题:没有达到我在 JsonModelBinder 中的所有断点。即使正在评估它,也不会在“返回 Task.Completed”时退出。JsonModelBinder 的生产代码包含更多反序列化传入字符串数据的逻辑。此代码包含失败逻辑,其中包含许多返回 Task.Completed 语句。使用此代码时,调试器将评估这些返回语句但继续前进,直到到达方法的末尾才返回,始终到达末尾。我正在使用 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 :-)    }// JsonModelBinderpublic 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;    }}
查看完整描述

1 回答

?
缥缈止盈

TA贡献2041条经验 获得超4个赞

我的一位同事遇到了同样的问题。经过大量调试和调查,我们发现这为他解决了问题。

  1. 右键单击 Visual Studio 中的解决方案并执行“清洁解决方案”。

  2. 手动删除项目的 obj 和 bin 文件夹的内容。

  3. 删除解决方案根目录中 .vs 文件夹的内容。(如果文件被锁定,请关闭 Visual Studio。)

最后一步似乎是重要的部分。


查看完整回答
反对 回复 2022-10-15
  • 1 回答
  • 0 关注
  • 68 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信