Finally, I ended doing this.
Have the method of the controller to unit test
public PageResult<User> GetAll(ODataQueryOptions<User> odataQuery)
Unit testing
// Arrange
var modelBuilder = new ODataConventionModelBuilder();
modelBuilder.EntitySet<User>("User");
var edmModel = modelBuilder.GetEdmModel();
const string routeName = "odata";
IEdmEntitySet entitySet = edmModel.EntityContainer.FindEntitySet("User");
ODataPath path = new ODataPath(new EntitySetSegment(entitySet));
var request = RequestFactory.Create("GET",
"http://localhost/api?$top=10&",
dataOptions => dataOptions.AddModel(routeName, edmModel));
request.ODataFeature().Model = edmModel;
request.ODataFeature().Path = path;
request.ODataFeature().PrefixName = routeName;
var oDataQueryContext = new ODataQueryContext(edmModel, typeof(User), new ODataPath());
var aDataQueryOptions = new ODataQueryOptions<User>(oDataQueryContext, request);
// Act
PageResult<User> users = _userController.GetAll(aDataQueryOptions);
You will need the helper class to generate HttpRequest
public static class RequestFactory
{
/// <summary>
/// Creates the <see cref="HttpRequest"/> with OData configuration.
/// </summary>
/// <param name="method">The http method.</param>
/// <param name="uri">The http request uri.</param>
/// <param name="setupAction"></param>
/// <returns>The HttpRequest.</returns>
public static HttpRequest Create(string method, string uri, Action<ODataOptions> setupAction)
{
HttpContext context = new DefaultHttpContext();
HttpRequest request = context.Request;
IServiceCollection services = new ServiceCollection();
services.Configure(setupAction);
context.RequestServices = services.BuildServiceProvider();
request.Method = method;
var requestUri = new Uri(uri);
request.Scheme = requestUri.Scheme;
request.Host = requestUri.IsDefaultPort ? new HostString(requestUri.Host) : new HostString(requestUri.Host, requestUri.Port);
request.QueryString = new QueryString(requestUri.Query);
request.Path = new PathString(requestUri.AbsolutePath);
return request;
}
it was taken from here https://github.com/OData/AspNetCoreOData/blob/master/test/Microsoft.AspNetCore.OData.Tests/Extensions/RequestFactory.cs
I hope it will help other who will face the same issue