Reputation: 809
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 :
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.
I'm using Moq, xUnit, VS2017, ASP.net Core 2.2.
// Simple fact
[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
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;
}
}
** Edit for Project Refs
Upvotes: 1
Views: 442
Reputation: 136
One of my colleagues encountered the same problem. After a lot of debugging and investigation, we found this fixed the problem for him.
The last step seems to be the important part.
Upvotes: 2