A coworker of mine, Tom Ceulemans, has provided a very nice solution to the mocking dilemma i posted earlier. I like this solution so much, i think it deserves its own post ![]()
The original question was basically: how do you do access a protected member in a test? I didn't want to make it public because it is a base class, and i was hoping to avoid making it protected internal and then exposing the internals to my tests assembly.
Tom's solution is very nice, and IMO, very clean as well. The trick is to create a derived abstract class from the class you want to test and then write the TestFixture as an inner class of the newly created class.
The test shown in the previous post would now look like this:
public abstract class MyAbstractClassTestWrapper : MyAbstractClass
{
[TestFixture]
public class MyAbstractClassTests
{
[Test]
public void CallsProtectedAbstractMethod()
{
var mocks = new MockRepository();
var myObject = mocks.DynamicMock<MyAbstractClassTestWrapper>();
myObject.Expect(m => m.DoSomethingSpecific()); // <= no more compile error
myObject.Replay();
myObject.DoSomethingInteresting();
myObject.VerifyAllExpectations();
}
}
}
I like it ![]()
Pingback: Dew Drop - July 22, 2008 | Alvin Ashcraft's Morning Dew