gpt4 book ai didi

c# - 为 .NET 5 核心 Controller 单元测试模拟或创建务实的 ODataQueryOptions

转载 作者:行者123 更新时间:2023-12-02 16:16:29 24 4
gpt4 key购买 nike

我正在将我们现有的 API 迁移到 .net 5,我面临单元测试迁移的问题。我想保留测试和测试 Controller ,但我找不到处理 ODataQueryOptions 的方法,因为此类已更改,我再也找不到创建 ODataQueryOptions 的方法。所有与我的问题相关的主题都已过时。

这是在迁移之前如何测试 Controller 的旧代码

_userController.Request = new HttpRequestMessage(HttpMethod.Get, new Uri("http://localhost/api?$orderby=UserName desc"));
_queryOptionsUser = new ODataQueryOptions<User>(_queryOptionsUser.Context, _userController.Request);
PageResult<UserDto> users = _userController.GetUsers(Constants.ValidOrgCode1, _queryOptionsUser);

如果有人能提供狙击手如何处理类似问题并能够测试 Controller ,我将不胜感激。

最佳答案

最后,我结束了这件事。

对controller的方法进行单元测试

public PageResult<User> GetAll(ODataQueryOptions<User> odataQuery) 

单元测试

// 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);

您将需要帮助程序类来生成 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;
}

取自这里https://github.com/OData/AspNetCoreOData/blob/master/test/Microsoft.AspNetCore.OData.Tests/Extensions/RequestFactory.cs

希望对遇到同样问题的 friend 有所帮助

关于c# - 为 .NET 5 核心 Controller 单元测试模拟或创建务实的 ODataQueryOptions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66554114/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com