6

Here is part of code implementation in parent class:

handler.FooUpdateDelegate += FooUpdate(OnFooUpdate);
protected abstract void OnFooUpdate(ref IBoo boo, string s);

I have in test method mocked handler:

Mock<IHandler> mHandler = mockFactory.Create<IHandler>();

This...

mHandler.Raise(x => x.FooUpdateDelegate += null, boo, s);

...is not working. It says:

System.ArgumentException : Could not locate event for attach or detach method Void set_FooUpdateDelegate(FooUpdate).

I want to raise OnFooUpdate so it triggers the code to be tested in child class.

Question: How can I raise delegate (not common event handler) with Moq?

If I missed the point completely, please enligten me.

1 Answer 1

8

It looks like you are trying to raise a delegate rather than an event. Is this so?

Is your code along the lines of this?

public delegate void FooUpdateDelegate(ref int first, string second);

public class MyClass {
    public FooUpdateDelegate FooUpdateDelegate { get; set; }
}

public class MyWrapperClass {

    public MyWrapperClass(MyClass myclass) {
        myclass.FooUpdateDelegate += HandleFooUpdate;
    }

    public string Output { get; private set; }

    private void HandleFooUpdate(ref int i, string s) {
            Output = s;
    }

}

If so, then you can directly invoke the myClass FooUpdateDelegate like so

[TestMethod]
public void MockingNonStandardDelegate() {

    var mockMyClass = new Mock<MyClass>();
    var wrapper = new MyWrapperClass(mockMyClass.Object);

    int z = 19;
    mockMyClass.Object.FooUpdateDelegate(ref z, "ABC");

    Assert.AreEqual("ABC", wrapper.Output);

}

EDIT: Adding version using interface

public interface IMyClass
{
    FooUpdateDelegate FooUpdateDelegate { get; set; }
}    

public class MyClass : IMyClass {
    public FooUpdateDelegate FooUpdateDelegate { get; set; }
}

public class MyWrapperClass {

    public MyWrapperClass(IMyClass myclass) {
        myclass.FooUpdateDelegate += HandleFooUpdate;
    }

    public string Output { get; private set; }

    private void HandleFooUpdate(ref int i, string s) {
            Output = s;
    }

}


[TestMethod]
public void MockingNonStandardDelegate()
{

   var mockMyClass = new Mock<IMyClass>();
   // Test fails with a Null Reference exception if we do not set up
   //  the delegate property. 
   // Can also use 
   //  mockMyClass.SetupProperty(m => m.FooUpdateDelegate);

   mockMyClass.SetupAllProperties();

   var wrapper = new MyWrapperClass(mockMyClass.Object);

   int z = 19;
   mockMyClass.Object.FooUpdateDelegate(ref z, "ABC");

   Assert.AreEqual("ABC", wrapper.Output);

}
3
  • Great answer. One thing I'd mention is that you may have to persist FooUpdateDelegate property, by calling mockMyClass.SetupAllProperties()
    – Eternal21
    Commented Jul 7, 2017 at 20:01
  • 1
    @Eternal21 The Mock<MyClass> is using the concrete implementation of the property from MyClass, so it runs fine as is. If we were mocking an interface (say, IMyClass), then there would be no concrete implementation and setting up the property on the mock would be needed.
    – AlanT
    Commented Jul 10, 2017 at 8:02
  • That was exactly my case.
    – Eternal21
    Commented Jul 10, 2017 at 11:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.