テストで多くの冗長コードを処理するために使用するコントローラー テスト ベースがあります。内部的には、最初に EF コードの s をTDataSource
含むインターフェイスであるジェネリック パラメーターを取ります。IDbSet
実行時に、テスト ベースは RhinoMocks を使用してモック TDataSource を構築し_MockDataSource = MockRepository.GenerateMock(Of TDataSource)()
ます。次に、リフレクションを使用してインターフェイスのすべての IDbSet プロパティを取得し、それらにモック データベース セットを割り当てます。(注:これは仮想クラスでうまく機能しますが、インターフェースだけでは機能しません)。
IDbSet(Of TEntity)
への呼び出しでプロパティの 1 つの値を設定しようとするとpropertyInfo.SetValue(dataSource, mockDbSet, Nothing)
、実際の値が設定されません。例外はありません。機能しないだけです。これは、オブジェクトdataSource
が実際にTDataSource
はタイプが指定するようなものではなくCastle.Proxies.IFakeDataSourceForTestingProxyf53f1730dba4492f8cafb9c731133d32
、への呼び出しを何らかの形で傍受しているためだと思いますSetValue
。どうすればこれを修正できますか。
以下の失敗したメソッドの完全なコード:
Protected Sub InjectTestData(ParamArray data() As IEnumerable)
Dim dataSourceType As Type = GetType(TDataSource)
Dim dataSourceProperties() As PropertyInfo = dataSourceType.GetProperties(BindingFlags.Public Or BindingFlags.Instance)
'match up supplied data and required data sets
For i As Integer = LBound(data) To UBound(data) Step 1
Dim [set] As IEnumerable = data(i)
Dim setType As Type = [set].GetType()
If Not setType.IsGenericType Then
Throw New ArgumentException(String.Format("Parameter {0} of the data array was not a generic IEnumerable, could not add to any IDbSet.", i))
End If
Dim modelType As Type = setType.GetGenericArguments(0)
Dim dbSetType As Type = GetType(IDbSet(Of )).MakeGenericType({modelType})
Dim foundDbSet As Boolean
For Each [property] As PropertyInfo In dataSourceProperties
If dbSetType.IsAssignableFrom([property].PropertyType) Then
Dim fakeDbSet As Object = CreateFakeDbSet(modelType, [set])
[property].SetValue(_MockDataSource, fakeDbSet, Nothing)
foundDbSet = True
End If
Next
If Not foundDbSet Then Throw New ArgumentException(String.Format("Could not find an IDbSet(Of {0}) property on {1} to match parameter {2}", modelType.Name, dataSourceType.Name, i))
Next
'add empty lists to any data sets which are still null
For Each [property] As PropertyInfo In dataSourceProperties
If [property].GetValue(_MockDataSource, Nothing) Is Nothing Then
Dim modelType As Type = [property].PropertyType.GetGenericArguments(0)
Dim fakeDbSet As Object = CreateFakeDbSet(modelType)
[property].SetValue(_MockDataSource, fakeDbSet, Nothing)
End If
Next
End Sub