Default Abstract Class Layout with Rhino

I am new to bullying, so this may be something that I have not yet collected, but I can not find a good example anywhere.

I am trying to argue that by default, any class that inherits from my abstract class will instantiate the collection in the constructor. Here's the abstract class:

public abstract class DataCollectionWorkflow : SequentialWorkflowActivity { private readonly DataSet _output = new DataSet(); private List<DataCollectionParameter> _params = null; public DataCollectionWorkflow() { _params = new List<DataCollectionParameter>(); } public virtual IList<DataCollectionParameter> Parameters { get { return _params; } set { _params = (List<DataCollectionParameter>)value; } } } 

How do I mock this with Rhino? If I make a GenerateMock<DataCollectionWorkflow> (or stub), the constructor starts and the layout of the private field " _params " is initialized, but the mock property " Parameters " is simply null.

Obviously, the generated layout subclass overrides the implementation of the property. Is there a way to exclude the Parameters property due to re-implementation?

Thanks.

+3
abstract-class mocking behavior rhino-mocks
source share
2 answers

Ok, I figured it out. Drop me as another victim for the intricacies of Rhino. This type of thing makes me want to move on to a simpler structure, maybe I'll check MoQ.

So the answer uses PartialMocks. I played briefly with generating a partial layout, but when I started the debugger, I noticed that the properties were not even null, they threw strange exceptions, so I did not look much deeper. I used short format AAA syntax.

It turns out that if I just put the layout in playback mode, the test works - the properties are used as they are (as it should be with a partial layout).

So here is the answer:

 [Test] public void ShouldCreateParameterListInConstructor() { var mockRepository = new MockRepository(); var mock = mockRepository.PartialMock<DataCollectionWorkflow>(); using ( mockRepository.Record() ) { } using (mockRepository.Playback()) { Assert.That(mock.Parameters, Is.Not.Null, "DataCollectionWorkflow base class didn't create new param collection"); } } 

I understand that this is a performance test, but in fact it is a simpler prelude to some behavioral testing, which includes this property, so I wanted this case to be a precondition. Hope this helps someone.

+5
source share

Try to make virtual _params

0
source share

All Articles