既存のコードにテスト カバレッジを追加しています。約 20 のクラス (すべて同じ基本クラスの派生クラス) があり、非常に類似した反復的なテスト メソッドになります。代わりに、私がやろうとしているのは、テスト対象の派生クラスの固有の詳細を処理するための抽象メソッドをいくつか公開する抽象テスト クラスを作成することです。
ToString オーバーライドをテストするまで、これはうまく機能していました。基本的に、テスト メソッド (抽象基本クラスにあることを思い出してください) は、期待される文字列値と、前述の抽象で提供されるジェネリック型のインスタンスで実行された ToString メソッドの結果とが等しいことをアサートしています。メソッド。ただし、予想していたように、ToString オーバーライドを呼び出すのではなく、代わりに標準オブジェクトの実装が呼び出され、オブジェクトの完全修飾名が返されます。
では、質問: ジェネリックに型指定されたインスタンスを処理するときに、クラスの ToString オーバーライドを呼び出すにはどうすればよいでしょうか?
ジェネリック型の基礎をよりよく理解することで、この質問に答えることができると思うので、簡潔にするために、コードを投稿していません。状況を過小評価している場合はお知らせください。関連するコードを追加します。
前もって感謝します!
編集==>コード:)
public abstract class KeyTestsBase<TKey, TKeyInterface>
where TKeyInterface : class
where TKey : class, IKeyParser<TKey>
{
protected readonly Mock<TKeyInterface> MockKey = new Mock<TKeyInterface>();
protected abstract string ExpectedStringValue { get; }
[Test]
public void ToString_produces_correct_string_value()
{
// Arrange
SetUpValidMock();
// Act
var key = BuildKey(MockKey.Object);
var keyValue = BuildKey(MockKey.Object).ToString();
// Assert
Assert.AreEqual(ExpectedStringValue, keyValue);
}
protected abstract void SetUpValidMock();
protected abstract TKey BuildKey(TKeyInterface keyInterface);
}
[TestFixture]
public class AdditiveProductKeyTests : KeyTestsBase<AdditiveProductKey, IAdditiveProductKey>
{
private const int VALID_PRODUCT_KEY = 123;
private const string VALID_PRODUCT_KEY_STRING = "123";
private const string VALID_PARSE_INPUT = "123";
#region Overrides of KeyTestsBase<AdditiveProductKey,IAdditiveProductKey>
protected override string ExpectedStringValue
{
get { return VALID_PRODUCT_KEY_STRING; }
}
protected override void SetUpValidMock()
{
MockKey.SetupGet(m => m.AdditiveProductKey_Id).Returns(VALID_PRODUCT_KEY);
}
protected override AdditiveProductKey BuildKey(IAdditiveProductKey keyInterface)
{
return new AdditiveProductKey(keyInterface);
}
#endregion
}
@Marc の疑いに反して、多くの Key クラスの 1 つの例を次に示します。ToString メソッドがオーバーライドされていることに注意してください。
public class AdditiveProductKey : KeyBaseClass<AdditiveProductKey>, IKey<AdditiveProduct>, IAdditiveProductKey
{
#region constructors and fields
private readonly int _id;
public AdditiveProductKey() : this(0) { }
public AdditiveProductKey(IAdditiveProductKey additiveProductKey)
:this(additiveProductKey.AdditiveProductKey_Id) { }
private AdditiveProductKey(int id)
{
_id = id;
}
#endregion
public Expression<Func<AdditiveProduct, bool>> FindByPredicate
{
get { return (p => p.Id == _id); }
}
protected override AdditiveProductKey ParseImplementation(string keyValue)
{
var id = int.Parse(keyValue);
return new AdditiveProductKey(id);
}
protected override string GetParseFailMessageImplementation()
{
return UserMessages.InvalidAdditiveProductKey;
}
public override string ToString()
{
return AdditiveProductKey_Id.ToString(CultureInfo.InvariantCulture);
}
#region Implementation of IAdditiveProductKey
public int AdditiveProductKey_Id { get { return _id; }}
#endregion
}
ToString_produces_correct_string_value テストの変数「key」の型情報を次に示します。
key.GetType()
{Name = "AdditiveProductKey" FullName = "RioValleyChili.Data.Utilities.Keys.AdditiveProductKey"}
[System.RuntimeType]: {Name = "AdditiveProductKey" FullName = "RioValleyChili.Data.Utilities.Keys.AdditiveProductKey"}
base {System.Reflection.MemberInfo}: {Name = "AdditiveProductKey" FullName = "RioValleyChili.Data.Utilities.Keys.AdditiveProductKey"}
Assembly: {RioValleyChili.Data.Utilities, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null}
AssemblyQualifiedName: "RioValleyChili.Data.Utilities.Keys.AdditiveProductKey, RioValleyChili.Data.Utilities, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
Attributes: Public | BeforeFieldInit
BaseType: {Name = "KeyBaseClass`1" FullName = "RioValleyChili.Business.Core.Keys.KeyBaseClass`1[[RioValleyChili.Data.Utilities.Keys.AdditiveProductKey, RioValleyChili.Data.Utilities, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]"}
ContainsGenericParameters: false
DeclaringMethod: 'key.GetType().DeclaringMethod' threw an exception of type 'System.InvalidOperationException'
DeclaringType: null
FullName: "RioValleyChili.Data.Utilities.Keys.AdditiveProductKey"
GenericParameterAttributes: 'key.GetType().GenericParameterAttributes' threw an exception of type 'System.InvalidOperationException'
GenericParameterPosition: 'key.GetType().GenericParameterPosition' threw an exception of type 'System.InvalidOperationException'
GenericTypeArguments: {System.Type[0]}
GUID: {81e8adb0-f899-358b-aa20-9bec8f96666d}
HasElementType: false
IsAbstract: false
IsAnsiClass: true
IsArray: false
IsAutoClass: false
IsAutoLayout: true
IsByRef: false
IsClass: true
IsCOMObject: false
IsConstructedGenericType: false
IsContextful: false
IsEnum: false
IsExplicitLayout: false
IsGenericParameter: false
IsGenericType: false
IsGenericTypeDefinition: false
IsImport: false
IsInterface: false
IsLayoutSequential: false
IsMarshalByRef: false
IsNested: false
IsNestedAssembly: false
IsNestedFamANDAssem: false
IsNestedFamily: false
IsNestedFamORAssem: false
IsNestedPrivate: false
IsNestedPublic: false
IsNotPublic: false
IsPointer: false
IsPrimitive: false
IsPublic: true
IsSealed: false
IsSecurityCritical: true
IsSecuritySafeCritical: false
IsSecurityTransparent: false
IsSerializable: false
IsSpecialName: false
IsUnicodeClass: false
IsValueType: false
IsVisible: true
MemberType: TypeInfo
Module: {RioValleyChili.Data.Utilities.dll}
Namespace: "RioValleyChili.Data.Utilities.Keys"
ReflectedType: null
StructLayoutAttribute: {System.Runtime.InteropServices.StructLayoutAttribute}
TypeHandle: {System.RuntimeTypeHandle}
TypeInitializer: null
UnderlyingSystemType: {Name = "AdditiveProductKey" FullName = "RioValleyChili.Data.Utilities.Keys.AdditiveProductKey"}
結果は次のとおりです。
key.ToString()
"RioValleyChili.Data.Utilities.Keys.AdditiveProductKey" //what i'm getting
((AdditiveProductKey)key).ToString()
"123" // what I'm expecting