How can you mock an object that both implements an interface and
inherits from a base class, when the method you're testing uses both?

I'm testing a class that has an object in it that inherits from an
abstract base class and also inherits from an interface - in the
example code below, it's the MyPresenter.OnLoad method. The method
sets event handlers for two events, one defined in the abstract base
class and one that's defined in the interface.

My test mocks the controller as DynamicMock<IMyController>. This lets
me set up expectations for an event handler to the event defined in
the interface - but my test fails on the event handler getting wired
up for the abstract class event. The mocked object throws an exception
when cast as a ControllerBase object.

System.InvalidCastException: Unable to cast object of type
'Castle.Proxies.IMyControllerProxyf09d4fa4a658425992d11645397d8e3f' to
type 'ControllerBase'..

Example code for this is below. I'd really appreciate any advice.

[TestMethod()]
public void OnViewLoaded_Test()
{
            // Arrange
        MyPresenter testPresenter = new MyPresenter();
        ControllerBase mockController = mocks.DynamicMock<IMyController>();

            // set up expectation that during the test, the class
being tested will add
       // a handler to the Controller's events and that this event
handler will not be null
        mockController.TaskSuspending += null;
        LastCall.Constraints(Rhino.Mocks.Constraints.Is.NotNull());
        ((ControllerBase))mockController.TaskSuspending += null;
        LastCall.Constraints(Rhino.Mocks.Constraints.Is.NotNull());
        mocks.ReplayAll();

            // Act
        testPresenter.OnViewLoaded();

            // Assert
        mocks.VerifyAll();
}


    public abstract class ControllerBase
    {
        /// <summary>
        /// Event raised when a task is being started.
        /// </summary>
        public event EventHandler StartTask;
    }

    public interface IMyController
    {
        event EventHandler ChangingTabs;
    }

    public class MyController : ControllerBase, IMyController
    {
        public event EventHandler ChangingTabs;

        protected virtual void OnChangingTabsChanged(EventArgs e)
        {
            if (ChangingTabs != null)
            {
                ChangingTabs(this, e);
            }
        }

    }

    public class MyPresenter
    {
        private MyController controller;

        public MyPresenter()
        {
            controller = new MyController();
        }

        public void OnLoad()
        {
            controller.ChangingTabs += this.HandleChangingTabs;
            ((ControllerBase)controller).StartTask +=
this.HandleStartTask;
        }

        public void HandleChangingTabs(object sender, EventArgs e)
        {
            // do something
        }

        public void HandleStartTask(object sender, EventArgs e)
        {
            // do something
        }

    }

-- 
You received this message because you are subscribed to the Google Groups 
"Rhino.Mocks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rhinomocks?hl=en.

Reply via email to