Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用Microsoft Fakes引用具有自身通用参数的存根?_C#_Visual Studio 2012_Microsoft Fakes - Fatal编程技术网

C# 如何使用Microsoft Fakes引用具有自身通用参数的存根?

C# 如何使用Microsoft Fakes引用具有自身通用参数的存根?,c#,visual-studio-2012,microsoft-fakes,C#,Visual Studio 2012,Microsoft Fakes,在我正在进行的一些单元测试中,我使用了Microsoft Fakes。我的界面如下所示: interface ISecuredItem<TChildType> where TChildType : class, ISecuredItem<TChildType> { SecurityDescriptor Descriptor { get; } IEnumerable<TChildType> Children { get; } } 接口ISeCr

在我正在进行的一些单元测试中,我使用了Microsoft Fakes。我的界面如下所示:

interface ISecuredItem<TChildType> where TChildType : class, ISecuredItem<TChildType>
{
    SecurityDescriptor Descriptor { get; }
    IEnumerable<TChildType> Children { get; }
}
接口ISeCreatedItem,其中TChildType:class,ISeCreatedItem
{
SecurityDescriptor描述符{get;}
IEnumerable子项{get;}
}
这方面的典型实现如下所示:

class RegistryKey : ISecuredItem<RegistryKey>
{
    public SecurityDescriptor Descriptor { get; private set; }
    public IEnumerable<RegistryKey> Children { get; }
}
类注册表项:ISeCreatedItem
{
公共安全描述符描述符{get;private set;}
公共IEnumerable子项{get;}
}

我想用这个界面与微软假货,并有它生成一个存根为我。问题是,Fakes使用的表单是
StubInterfaceNameHere
,因此在上面的示例中,您最终尝试执行类似
StubISecuredItem的操作。经过一些实验,我找到了一个有效的解决方案,尽管它不是最优雅的

这是您的常规代码:

public interface ISecuredItem<TChildType>
    where TChildType : ISecuredItem<TChildType>
{
    SecurityDescriptor Descriptor { get; }
    IEnumerable<TChildType> Children { get; }
}
公共接口ISecuredItem
其中TChildType:IseCreatedItem
{
SecurityDescriptor描述符{get;}
IEnumerable子项{get;}
}
在测试项目中,创建stubImpleStation接口

public interface StubImplemtation : ISecuredItem<StubImplemtation> { }
public接口stubemplotation:ISecuredItem{}
然后在单元测试中,您可以执行以下操作:

var securedItemStub = new StubISecuredItem<StubImplemtation>
                          {
                              ChildrenGet = () => new List<StubImplemtation>(),
                              DescriptorGet = () => new SecurityDescriptor()
                          };

var children = securedItemStub.ChildrenGet();
var descriptor = securedItemStub.DescriptorGet();
var securedItemStub=new StubISecuredItem
{
ChildrenGet=()=>新列表(),
DescriptorTarget=()=>new SecurityDescriptor()
};
var children=securedItemStub.ChildrenGet();
var descriptor=securedItemStub.DescriptorTarget();

如果没有问题,您可以跳过整个
StubImplementation
并使用
RegistryKey

这肯定会奏效,但需要在测试项目中将接口实现为stub/mock。我不知道这是不是个主意…我可以想象这不是个主意。。但我看不到任何其他解决方案。当您查看Fakes框架生成的源代码时,泛型参数的类型是ISeCreatedItem,而不是StubISeCreatedItem。我几乎可以说这是框架中的一个bug。测试项目中的实现可以让所有方法抛出一个NotImplementedException,因此创建它不需要花费太多时间。我知道,我完全同意。我脑海中自动浮现的答案就是你提到的那一个。没错,这是可行的,但完全击败了fakes女士的观点。关键是不需要显式实现存根类。。。但我明白你的意思。是的,我明白这不是最好的解决办法。但你不必完全实现它!您仍然可以对每个方法进行存根,并对其执行任何操作。我认为这是你能找到的最好的方法。