如何设置MS Fakes对象的返回值? [英] How to set the return value of MS Fakes object?
问题描述
我已经用OData V4 Client Code Generator
生成了OData客户端代码.如果没有MS Fakes,则无法对生成的代码进行单元测试,因此我从中生成了伪造的程序集.现在我有一个问题,如何实际设置方法的返回值.
I have generated an OData Client code with the OData V4 Client Code Generator
. The generated code cannot be unit tested without MS Fakes so I generated a fake assembly from it. Now I have a problem of how to actually set the return value of the methods.
生成的代码中的核心"类称为System
:
The "core" class in the generated code is called 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表达式作为参数.
Now as you can see the property Salesorders
is DataServiceQuery<Salesorder>
which takes a Linq expression as a parameter.
我尝试手动设置查询,但是它不起作用,并且在测试用例中指定实际查询似乎有点多余.基本上,我需要的是一种返回List(或Enumrable)的方法,就像我可以使用Moq时那样.
I've tried to set the query by hand, but it doesn't work and also it seem a bit redundant to specify the actual query in a test case. Basically all I need is the method to return a List (or Enumrable) like I would do it if I could use Moq.
我发现了一篇有关将Fakes与较旧的CRM代码生成器一起使用的旧文章,但是在这种情况下,它对我没有多大帮助(
I found an old article about using Fakes with the older CRM code generator, but it doesn't help me in this case much (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;
}
这篇关于如何设置MS Fakes对象的返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!