Rhino 用 CompositionContainer 模拟异常

本文关键字:异常 模拟 CompositionContainer Rhino | 更新日期: 2023-09-27 18:35:55

[TestFixture]
public class UnitListViewModelTests
{
    private CompositionContainer _container;
    private INHDataService _nhDataService;
    private INHUnitOfWork _nhUoW;
    [SetUp]
    [TestCase]
    public void create_container()
    {
        _container = MockRepository.GeneratePartialMock<CompositionContainer>();
        _nhDataService = MockRepository.GenerateMock<INHDataService>();
        _nhUoW = MockRepository.GenerateMock<INHUnitOfWork>();
        _container.Expect(x => x.GetExportedValue<INHDataService>()).IgnoreArguments().Return(_nhDataService);
    }
}

此代码在 _container.Expect... 处引发异常。

SetUp : System.InvalidOperationException : Previous method 'IEnumerator.MoveNext();' requires a return value or an exception to throw.
   at Rhino.Mocks.Impl.RecordMockState.AssertPreviousMethodIsClose()
   at Rhino.Mocks.Impl.RecordMockState.MethodCall(IInvocation invocation, MethodInfo method, Object[] args)
   at Rhino.Mocks.MockRepository.MethodCall(IInvocation invocation, Object proxy, MethodInfo method, Object[] args)
   at Rhino.Mocks.Impl.Invocation.Actions.RegularInvocation.PerformAgainst(IInvocation invocation)
   at Rhino.Mocks.Impl.RhinoInterceptor.Intercept(IInvocation invocation)
   at Castle.DynamicProxy.AbstractInvocation.Proceed()
   at Castle.Proxies.IEnumerator`1Proxy15c4de116cb145039cc424d2b9ced840.IDisposable.Dispose()
   at Microsoft.Internal.Collections.CollectionServices.GetCardinality[T](IEnumerable`1 source)
   at System.ComponentModel.Composition.ExportServices.CheckCardinality(ImportDefinition definition, IEnumerable`1 exports)
   at System.ComponentModel.Composition.Hosting.ExportProvider.TryGetExportsCore(ImportDefinition definition, AtomicComposition atomicComposition, IEnumerable`1& exports)
   at System.ComponentModel.Composition.Hosting.ExportProvider.GetExports(ImportDefinition definition, AtomicComposition atomicComposition)
   at System.ComponentModel.Composition.Hosting.ExportProvider.GetExportsCore(Type type, Type metadataViewType, String contractName, ImportCardinality cardinality)
   at System.ComponentModel.Composition.Hosting.ExportProvider.GetExportedValueCore[T](String contractName, ImportCardinality cardinality)
   at System.ComponentModel.Composition.Hosting.ExportProvider.GetExportedValue[T](String contractName)
   at System.ComponentModel.Composition.Hosting.ExportProvider.GetExportedValue[T]()
   at MIB.Test.ViewModels.UnitListViewModelTests.<create_container>b__0(CompositionContainer x) in E:'Work'MIB'MIB.Test'ViewModels'UnitListViewModelTests.cs:line 29#0
   at Rhino.Mocks.RhinoMocksExtensions.Expect[T,R](T mock, Function`2 action)
   at MIB.Test.ViewModels.UnitListViewModelTests.create_container() in E:'Work'MIB'MIB.Test'ViewModels'UnitListViewModelTests.cs:line 29#1

编辑:我也试过这个,但它仍然抛出一个异常:

_container.Stub(x => x.GetExportedValue<INHDataService>()).IgnoreArguments().Return(_nhDataService);

Rhino 用 CompositionContainer 模拟异常

我猜CompositionContainer.GetExportedValue()方法不是虚拟的。