Testing abstract classes is a little more difficult than testing an Interface, since there may be some actual business rules in the abstract class you are trying to test.
If you are reading this page, I will presume that you do not have a concrete class that you can do your testing on, or that you are interested in isolating your tests of the super class from the sub classes.
Let's assume you are trying to test this class (This is just for show, would probably be wiser to make a class separately to collect the users and inject that as dependency into this class)
public abstract class MessageBase {
private List<User> _receivers = new List<User>();
public void Send() {
//Some setup
DetermineReceivers();
SendMessages();
}
private void SendMessages() {
//Lots of logic
}
protected abstract void DetermineReceivers();
protected void AddReceiver(Group g) {
//Lots of logic <---- Test this
}
}
It is not possible to use DynamicMocks or any other methods of Rhino Mocks to directly test this, because the methods we want to test are not accessible from outside of the class and nothing in the abstract class actually uses the logic we want to test.
Instead we can make a Rhino Mockable mock class
TestFixture
public sealed class MessageBaseTester {
public abstract class MockMessageMocker
{
public abstract List<Group> RecipientsGroups { get; set; }
}
private class MockMessage : MessageBase {
private MockMessageMocker _mock;
public MockMessage(MockMessageMocker mock) {
_mock = mock;
}
protected override void DetermineReceivers() {
if (_mock.RecipientsGroups != null)
foreach(Group g in _mock.RecipientsGroups)
base.AddReceiver(g);
}
}
}
And then our tests can follow the usual pattern, with just one small change where an SuTHelper is added to assist:
Test
public void ShouldblablablaWhenBlabla() {
var SuThelper = mocks.Stub();
var SuT = new MockMessage(SuThelper);
using (mocks.Record())
{
Expect.Call(SuTHelper.RecipientsGroups).Return(new List<Group>{ new Group(...) });
}
using (mocks.Playback())
{
SuT.Send();
}
}