gpt4 book ai didi

asp.net-mvc - Web API、OData、$inlinecount 和测试

转载 作者:行者123 更新时间:2023-12-03 18:19:46 28 4
gpt4 key购买 nike

我以前有一个 Web API Controller ,如下所示:

    public IQueryable<ApiDesignOverview> GetList(
string brandIds = "",
string categoryIds = "",
string query = "",
string categoryOp = "or")

我听说 OData NuGet 包现在支持 $inlinecount OData 参数,所以我尝试使用来自 http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/supporting-odata-query-options 的说明添加它- 我不想批发使用 OData,因为这将需要大量重新构建应用程序,所以我选择了 PageResult<T>选项。

所以现在我的 Controller 看起来像这样:
    public PageResult<ApiDesignOverview> GetList(
ODataQueryOptions<ApiDesignOverview> options,
string brandIds = "",
string categoryIds = "",
string query = "",
string categoryOp = "or")

我现在的问题是:
  • 如何模拟 ODataQueryOptions 进行单元测试?
  • 如果它们不能被 mock ,我如何创建一个?我需要一个 ODataQueryContext构建一个,这需要一个 Microsoft.Data.Edm.IEdmModel ,这需要……什么?我找不到这方面的任何文档。

  • 真的,如果我能像以前一样从 Controller 签名中删除 ODataQueryOptions 会更好。这可能吗?

    最佳答案

    如果您不想(或不能像我一样)不想使用 ODataQueryOptions 和 PageResult,那么您可以通过以下方式为单元测试创​​建 ODataQueryOptions 实例:

    //arrange
    var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/MyProject/api/Customers?$filter=CustomerID eq 1");
    var controller = new CustomersController
    {
    Request = request
    };

    ODataModelBuilder modelBuilder = new ODataConventionModelBuilder();
    modelBuilder.EntitySet<Customer>("Customers");
    var opts = new ODataQueryOptions<Customer>(new ODataQueryContext(modelBuilder.GetEdmModel(),typeof(Customer)), request);

    //act
    var result = controller.Get(opts);

    //assert
    Assert.AreEqual(1, result.Items.First().CustomerID);

    关于asp.net-mvc - Web API、OData、$inlinecount 和测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15790695/

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