Microsoft Fakesを使用して自分自身の一般的なパラメータを持つスタブをどのように参照しますか?
-
12-12-2019 - |
質問
私が取り組んでいるいくつかの単体テストでMicrosoft Fakesを使っています。私のインターフェースは次のようになります:
interface ISecuredItem<TChildType> where TChildType : class, ISecuredItem<TChildType>
{
SecurityDescriptor Descriptor { get; }
IEnumerable<TChildType> Children { get; }
}
.
これの典型的な実装は次のように見えます。
class RegistryKey : ISecuredItem<RegistryKey>
{
public SecurityDescriptor Descriptor { get; private set; }
public IEnumerable<RegistryKey> Children { get; }
}
.
このインターフェースをマイクロソフトの偽造品と一緒に使い、それは私のためにスタブを生成しました。問題は、フォームファークが使用するのが一般的なものです。
これは可能ですか?もしそうなら、このようにしておすすめを使うのですか?
解決
実験の後、私は最もエレガントではありませんが、働く解決策を見つけました。
これはあなたの通常のコードです:
public interface ISecuredItem<TChildType>
where TChildType : ISecuredItem<TChildType>
{
SecurityDescriptor Descriptor { get; }
IEnumerable<TChildType> Children { get; }
}
.
テストプロジェクトでは、標準集中インターフェイスを作成します。
public interface StubImplemtation : ISecuredItem<StubImplemtation> { }
.
それからあなたの単体テストでは、次のことができます。
var securedItemStub = new StubISecuredItem<StubImplemtation>
{
ChildrenGet = () => new List<StubImplemtation>(),
DescriptorGet = () => new SecurityDescriptor()
};
var children = securedItemStub.ChildrenGet();
var descriptor = securedItemStub.DescriptorGet();
.
StubImplementation
全体をスキップして、問題がない場合はRegistryKey
を使用できます。
所属していません StackOverflow