1

Is it possible to check via unit testing, if the event is subscribed or not when mocking is not possible.

I have following class "InteractiveEventManager" which takes a class called DrawingArea. The class implements IEventManager which has two methods - Subscribe() and UnSubscribe();

Subscribe method subscribes custom events from DrawingArea class.

Now I am not sure how to check if the Subscribe method has subscribed those events?

internal interface IEventManager
{
    void Subscribe();
    void UnSubscribe();
}

public class InteractiveEventManager: IEventManager
{
    private readonly DrawingArea _drawingArea;   
        public InteractiveEventManager(DrawingArea drawingArea)
    {
        _drawingArea = drawingArea;
    }

    public void Subscribe()
    {
        _drawingArea.MouseMove += DrawingAreaMouseMove;
        _drawingArea.PreviewMouseDown += DrawingAreaPreviewMouseDown;
    }

    private void DrawingAreaPreviewMouseDown(object sender, MouseButtonEventArgs e)
    {
        _drawingArea.Chart.InteractiveCurvesArea.ProcessMouseDown(
            e.GetPosition(_drawingArea));
    }

    private void DrawingAreaMouseMove(object sender, MouseEventArgs e)
    {
        _drawingArea.Chart.InteractiveCurvesArea.ProcessMouseMove(
            e.GetPosition(_drawingArea));
    }
}

I have written following test but it does not work. Since, DrawingArea is a class(not an interface), I think we cannot call AssertWasCalled. Can you please help.

[Test]
public void When_register_then_drawing_area_mouse_move_is_registered()
{
    DrawingArea drawingArea = new DrawingArea();
    InteractiveEventManager interactiveEventManager = 
        new InteractiveEventManager(drawingArea);
    interactiveEventManager.Subscribe();

    drawingArea.AssertWasCalled(x => x.MouseMove += 
        Arg<MouseEventHandler>.Is.Anything);
}

Thanks.

Chris Tavares
  • 29,165
  • 4
  • 46
  • 63
App
  • 346
  • 3
  • 9
  • 1
    I would first really try to go the route of abstracting the DrawingArea class behind an interface. Otherwise, this is not really unit testing but integration testing. – tjugg Sep 18 '17 at 18:58
  • For unit testing it is perfectly legal to use Reflection, so if `DrawingArea` is out of your control, you can employ this technique: https://stackoverflow.com/a/28503102/2057955 – zaitsman Sep 17 '19 at 06:43

0 Answers0