We have a base class providing some default implementation for INotifyPropertyChanged
(this class is used by many other classes and cannot be easily changed):
public class ObservableBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
// this is virtual so derived classes can override it (rarely used, but it is used)
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
Now I have an interface and an abstract base class deriving from ObservableBase
and implementing that interface providing some default implementations (mainly for the properties):
public interface INamedTrigger
{
string Name { get; }
void Execute();
}
public abstract class ObservableNamedTriggerBase : ObservableBase, INamedTrigger
{
private string _Name;
public string Name
{
get { return _Name; }
set { _Name = value; OnPropertyChanged("Name"); }
}
public abstract void Execute();
}
Now I want to unit test the default implementations of ObservableNamedTriggerBase
(using NUnit and RhinoMocks):
[TestFixture]
public class ObservableNamedTriggerBaseTest
{
[Test]
public void TestNameChangeRaisesPropertyChanged()
{
var prop = "";
var mocks = new MockRepository();
var namedBase = mocks.PartialMock<ObservableNamedTriggerBase>();
namedBase.PropertyChanged += (s, e) => prop = e.PropertyName;
namedBase.Name = "Foo";
Assert.AreEqual("Name", prop);
}
}
Unfortunately this test fails as Rhino seems to override the virtual OnPropertyChanged
method from ObservableBase
.
This SO question and the Rhino docs indicate that PartialMock
should exactly not do this. On the other hand the answer to this SO question indicates that Rhino always overrides virtual methods regardless of the type of mock.
So is the documentation wrong? And how do I go about testing this correctly?
Update: If I create my own mock class providing dummy implementations for just the abstract methods then the test passes but I'd like to use Rhino mocks to save me the work of exactly doing that.