如何设置 MS Fakes 对象的 return 值?
How to set the return value of MS Fakes object?
我已经使用 OData V4 Client Code Generator
生成了一个 OData 客户端代码。生成的代码不能在没有 MS Fakes 的情况下进行单元测试,所以我从中生成了一个假的程序集。现在我遇到了如何实际设置方法的 return 值的问题。
生成代码中的"core"class调用System
:
[global::Microsoft.OData.Client.OriginalNameAttribute("System")]
public partial class System : global::Microsoft.OData.Client.DataServiceContext
{
/// <summary>
/// Initialize a new System object.
/// </summary>
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.OData.Client.Design.T4", "2.4.0")]
public System(global::System.Uri serviceRoot) :
base(serviceRoot, global::Microsoft.OData.Client.ODataProtocolVersion.V4)
{
this.ResolveName = new global::System.Func<global::System.Type, string>(this.ResolveNameFromType);
this.ResolveType = new global::System.Func<string, global::System.Type>(this.ResolveTypeFromName);
this.OnContextCreated();
this.Format.LoadServiceModel = GeneratedEdmModel.GetInstance;
this.Format.UseJson();
}
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.OData.Client.Design.T4", "2.4.0")]
[global::Microsoft.OData.Client.OriginalNameAttribute("salesorders")]
public global::Microsoft.OData.Client.DataServiceQuery<Salesorder> Salesorders
{
get
{
if ((this._Salesorders == null))
{
this._Salesorders = base.CreateQuery<Salesorder>("salesorders");
}
return this._Salesorders;
}
}
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.OData.Client.Design.T4", "2.4.0")]
private global::Microsoft.OData.Client.DataServiceQuery<Salesorder> _Salesorders;
... continues from here and contains all the strongly typed classes...
}
现在您可以看到 属性 Salesorders
是 DataServiceQuery<Salesorder>
,它将 Linq 表达式作为参数。
我尝试手动设置查询,但它不起作用,而且在测试用例中指定实际查询似乎有点多余。基本上,我所需要的只是 return 列表(或可枚举)的方法,就像我可以使用起订量时会做的那样。
编辑:我发现了一篇关于在旧 CRM 代码生成器中使用 Fakes 的旧文章,但在这种情况下它对我帮助不大 (https://zhongchenzhou.wordpress.com/2012/07/10/dynamics-crm-2011-unit-test-part-2-microsoft-fakes-with-linq-query)
_client = new ODataClient.Microsoft.Dynamics.CRM.Fakes.StubSystem(new System.Uri(...
_dao = new DataAccess.DataAccess(_client);
using (ShimsContext.Create())
{
var query = from a in _client.Salesorders select a;
ODataClient.Microsoft.Dynamics.CRM.Fakes.ShimSystem.AllInstances.SalesordersGet = (c) =>
{
return new Microsoft.OData.Client.DataServiceQuery<Salesorder>( // how?
};
// This fails _dao.GetSalesordersByAccountAndContactId(Guid.NewGuid().ToString(), Guid.NewGuid().ToString());
}
using (ShimsContext.Create())
{
var client = new ODataClient.Microsoft.Dynamics.CRM.Fakes.ShimSystem();
IDataAccess dao = new DataAccess.DataAccess(client);
var salesorders = new List<Salesorder>
{
new Salesorder()
};
IQueryable<Salesorder> queryableData = salesorders.AsQueryable();
var queryShim = new Microsoft.OData.Client.Fakes.ShimDataServiceQuery<Salesorder>();
queryShim.ExpressionGet = () => queryableData.Expression;
queryShim.ElementTypeGet = () => queryableData.ElementType;
queryShim.ProviderGet = () => queryableData.Provider;
queryShim.GetEnumerator = () => queryableData.GetEnumerator();
DataClient.Microsoft.Dynamics.CRM.Fakes.ShimSystem.SalesordersGet = () => queryShim;
}
我已经使用 OData V4 Client Code Generator
生成了一个 OData 客户端代码。生成的代码不能在没有 MS Fakes 的情况下进行单元测试,所以我从中生成了一个假的程序集。现在我遇到了如何实际设置方法的 return 值的问题。
生成代码中的"core"class调用System
:
[global::Microsoft.OData.Client.OriginalNameAttribute("System")]
public partial class System : global::Microsoft.OData.Client.DataServiceContext
{
/// <summary>
/// Initialize a new System object.
/// </summary>
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.OData.Client.Design.T4", "2.4.0")]
public System(global::System.Uri serviceRoot) :
base(serviceRoot, global::Microsoft.OData.Client.ODataProtocolVersion.V4)
{
this.ResolveName = new global::System.Func<global::System.Type, string>(this.ResolveNameFromType);
this.ResolveType = new global::System.Func<string, global::System.Type>(this.ResolveTypeFromName);
this.OnContextCreated();
this.Format.LoadServiceModel = GeneratedEdmModel.GetInstance;
this.Format.UseJson();
}
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.OData.Client.Design.T4", "2.4.0")]
[global::Microsoft.OData.Client.OriginalNameAttribute("salesorders")]
public global::Microsoft.OData.Client.DataServiceQuery<Salesorder> Salesorders
{
get
{
if ((this._Salesorders == null))
{
this._Salesorders = base.CreateQuery<Salesorder>("salesorders");
}
return this._Salesorders;
}
}
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.OData.Client.Design.T4", "2.4.0")]
private global::Microsoft.OData.Client.DataServiceQuery<Salesorder> _Salesorders;
... continues from here and contains all the strongly typed classes...
}
现在您可以看到 属性 Salesorders
是 DataServiceQuery<Salesorder>
,它将 Linq 表达式作为参数。
我尝试手动设置查询,但它不起作用,而且在测试用例中指定实际查询似乎有点多余。基本上,我所需要的只是 return 列表(或可枚举)的方法,就像我可以使用起订量时会做的那样。
编辑:我发现了一篇关于在旧 CRM 代码生成器中使用 Fakes 的旧文章,但在这种情况下它对我帮助不大 (https://zhongchenzhou.wordpress.com/2012/07/10/dynamics-crm-2011-unit-test-part-2-microsoft-fakes-with-linq-query)
_client = new ODataClient.Microsoft.Dynamics.CRM.Fakes.StubSystem(new System.Uri(...
_dao = new DataAccess.DataAccess(_client);
using (ShimsContext.Create())
{
var query = from a in _client.Salesorders select a;
ODataClient.Microsoft.Dynamics.CRM.Fakes.ShimSystem.AllInstances.SalesordersGet = (c) =>
{
return new Microsoft.OData.Client.DataServiceQuery<Salesorder>( // how?
};
// This fails _dao.GetSalesordersByAccountAndContactId(Guid.NewGuid().ToString(), Guid.NewGuid().ToString());
}
using (ShimsContext.Create())
{
var client = new ODataClient.Microsoft.Dynamics.CRM.Fakes.ShimSystem();
IDataAccess dao = new DataAccess.DataAccess(client);
var salesorders = new List<Salesorder>
{
new Salesorder()
};
IQueryable<Salesorder> queryableData = salesorders.AsQueryable();
var queryShim = new Microsoft.OData.Client.Fakes.ShimDataServiceQuery<Salesorder>();
queryShim.ExpressionGet = () => queryableData.Expression;
queryShim.ElementTypeGet = () => queryableData.ElementType;
queryShim.ProviderGet = () => queryableData.Provider;
queryShim.GetEnumerator = () => queryableData.GetEnumerator();
DataClient.Microsoft.Dynamics.CRM.Fakes.ShimSystem.SalesordersGet = () => queryShim;
}