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

如何使用 MSFakes 模拟 ODataClient 的 Expand 方法?

如何使用 MSFakes 模拟 ODataClient 的 Expand 方法?

C#
MMMHUHU 2021-06-14 15:26:51
我已经从 Dynamics 365 元数据生成了一个 OData 客户端。我正在为业务逻辑编写单元测试,我想用自定义数据模拟 REST API。之前问过如何设置ODataClient的假返回数据,我找到了答案:如何设置MS Fakes对象的返回值?一切正常,直到我不得不使用 Expand 关键字(使用OData Library 中的Expand lambda查询嵌套数据)。例如,当与 Faked ODataClient 程序集一起使用时,此查询失败并显示 NullReferenceException:IQueryable<Insurance> insurancesQuery = from i in _client.Insurances.Expand(i => i.Account_id)select i;如果没有 Expand lambda,一切都会按预期进行。如果我这样做:IQueryable<Insurance> insurancesQuery = from i in _client.Insurances.Expand("account_id")select i;然后我得到NullReferenceException一个堆栈跟踪:> at lambda_method(Closure , Ecr_insurance )    at> System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()    at> System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)   > at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)我知道问题在于我没有在某处设置 Shim 或 Stub,但我找不到正确的替换方法。我试图阅读 OData 源代码,但还没有找到任何有用的东西。我试图设置 Accounts ( ShimSystem.AccountsGet)的 Getter 方法,但它没有做任何事情。任何想法如何进一步解决这个问题?
查看完整描述

1 回答

?
梦里花落0921

TA贡献1772条经验 获得超6个赞

事实证明,我还必须ExpandString在 OData 客户端的System类上设置shim 方法,以返回与提供程序本身相同的数据。


查看完整回答
反对 回复 2021-06-20
  • 1 回答
  • 0 关注
  • 114 浏览

添加回答

举报

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