Question

I'm writing some unit tests for a class (in example called ClassUnderTest) which has a property binding to a property from its model. As the model is defined with a interface (IModel), the model is mocked by a Rhino Mocks mock.

Sample code:

Interface for Model:

public interface IModel : INotifyPropertyChanged
{
   event PropertyChangedEventHandler PropertyChanged;
   int SourceValue { get; set; }
}

class which shall be tested:

public class ClassUnderTest : DependencyObject
{
   private IModel model;

   public int TargetValue
   {
      get { return (int)GetValue(TargetValueProperty); }
      set { SetValue(TargetValueProperty, value); }
   }

   public static readonly DependencyProperty TargetValueProperty = DependencyProperty.Register("TargetValue", typeof(int), typeof(ClassUnderTest), new PropertyMetadata(0));

   public ClassUnderTest(IModel model)
   {
      this.model = model;
      var b = new Binding("SourceValue") { Source = this.model, Mode = BindingMode.OneWay };
      BindingOperations.SetBinding(this, TargetValueProperty, b);
   }
}

Unit test method:

[TestMethod]
public void TestMethod()
{
   var repo = new MockRepository();
   var modelMock = repo.StrictMock<IModel>();
   const int expectedValue = 4;
   IEventRaiser propertyChanged;

   using (repo.Record())
   {
      propertyChanged = Expect.Call(delegate { modelMock.PropertyChanged += null; }).IgnoreArguments().GetEventRaiser();
      Expect.Call(() => modelMock.PropertyChanged -= null).Constraints(Is.NotNull()).Repeat.Any();
      Expect.Call(modelMock.SourceValue).Return(expectedValue).Repeat.Any();             
   }

   using (repo.Playback())
   {
      var cuT = new ClassUnderTest(modelMock);
      propertyChanged.Raise(modelMock, new PropertyChangedEventArgs("SourceValue"));
      Assert.AreEqual(expectedValue, cuT.TargetValue);
   }
}

Running the test method, all works fine. Raising the property changed event on the model mock provokes a change in the ClassUnderTest class too.

The problem I have now is only observed in debug mode. At the end of the test I get a InvalidOperationException:

This action is invalid when the mock object is in verified state.:
at Rhino.Mocks.Impl.VerifiedMockState.MethodCall(IInvocation invocation, MethodInfo method, Object[] args)
at Rhino.Mocks.MockRepository.MethodCall(IInvocation invocation, Object proxy, MethodInfo method, Object[] args)
at Rhino.Mocks.Impl.RhinoInterceptor.Intercept(IInvocation invocation)
at Castle.DynamicProxy.AbstractInvocation.Proceed()
at IModelProxy2856c01157e54c29a4a7328a5a7ef52a.remove_PropertyChanged(PropertyChangedEventHandler value)
at System.ComponentModel.PropertyChangedEventManager.StopListening(Object source)
at System.ComponentModel.PropertyChangedEventManager.Purge(Object source, Object data, Boolean purgeAll)
at MS.Internal.WeakEventTable.Purge(Boolean purgeAll)
at MS.Internal.WeakEventTable.WeakEventTableShutDownListener.OnShutDown(Object target, Object sender, EventArgs e)
at MS.Internal.ShutDownListener.HandleShutDown(Object sender, EventArgs e)

As I understand this is because the binding provokes a un-subscription by disposing all the objects at the end of the test, but the mock is already in the verify-state and doesn't allow any interaction anymore.

So here my QUESTION: Is there a way to avoid this exception, or the un-subscription. Or I'am using the Rhino Mocks in incorrect manner?

Thanks for helping

Was it helpful?

Solution

Did you try this:

using (repo.Playback())
{
    using (var cuT = new ClassUnderTest(modelMock))
    {
        propertyChanged.Raise(modelMock, new PropertyChangedEventArgs("SourceValue"));
        Assert.AreEqual(expectedValue, cuT.TargetValue);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top