2

I have an Interface which has a property on another interface,

public interface IInnerInterface
{
    event EventHandler OnCompleted;
}

public class InnerClassImplementation : IInnerInterface
{
    public event EventHandler OnCompleted;

    private void CompletedState()
    {
        OnCompleted?.Invoke(this, new EventArgs());
    }
}

public interface IOuterInterface
{
    IInnerInterface InnerInterface { get; }
}

public class Main : IOuterInterface
{
    public IInnerInterface InnerInterface { get; }

    public bool IsOperationComplete { get; set; }

    public Main(IInnerInterface innerInterface)
    {
        InnerInterface = innerInterface;
        InnerInterface.OnCompleted += InnerInterface_OnCompleted;
    }

    private void InnerInterface_OnCompleted(object sender, EventArgs e)
    {
        IsOperationComplete = true;
    }
}

I am trying to test the Main class. One of the test case is to validate the Handler method for the event.

I tried the following code implementation to test,

[TestClass]
public class MainTest
{
    private Mock<IInnerInterface> _innerInterfaceMock;
    private Main _main;

    [TestInitialize]
    public void Initialize()
    {
        _innerInterfaceMock = new Mock<IInnerInterface>();
        _main = new Main(_innerInterfaceMock.Object);
    }

    [TestMethod]
    public void OnCompleted_ShouldDoSomething()
    {
        //Act
        _main.Raise(m=>m.InnerInterface.OnCompleted+=null, new EventArgs());

        //Assert
        _main.IsOperationComplete.Should().BeTrue();

    }
}

I am getting the following error,

Test method Samples.MainTest.OnCompleted_ShouldDoSomething threw exception: Telerik.JustMock.Core.MockException: Unable to deduce which event was specified in the parameter. at Telerik.JustMock.Core.Behaviors.RaiseEventBehavior.RaiseEventImpl(Object instance, EventInfo evt, Object[] args) at Telerik.JustMock.Core.ProfilerInterceptor.GuardInternal(Action guardedAction) at Samples.MainTest.OnCompleted_ShouldDoSomething()

Don't know what am I doing wrong?

Raajkumar
  • 857
  • 2
  • 13
  • 26
  • So, tell me, are you using [Moq](https://github.com/Moq/moq4/wiki/Quickstart) or are you using [JustMock](https://www.telerik.com/products/mocking.aspx)? make up your mind and fix the tags! – quetzalcoatl Jan 31 '18 at 17:16
  • I am trying to use justmock – Raajkumar Jan 31 '18 at 17:16
  • I created a sample from the real code. As part of that I forgot to remove the namespace. On the title, I accepted the change from @quetzalcoatl – Raajkumar Jan 31 '18 at 17:23
  • @quetzalcoatl I'm not sure, actually; I didn't make that change. I was wondering the same thing when I went back to look at the diff. Looking at the history it seems it was in the original, and then the OP replaced it with Samples -- we must have been making edits around the same time and my change overwrote his. Raajkumar feel free to switch that back, apologies. – Mike Paxton Jan 31 '18 at 17:24
  • Sure will update it. – Raajkumar Jan 31 '18 at 17:25
  • That was truly interesting set of glitches in editing :) – quetzalcoatl Jan 31 '18 at 17:27

1 Answers1

3

You shouldn't raise the event from your SUT (Main), raise it straight from the IInnerInterface mock:

_innerInterfaceMock.Raise(o => o.OnCompleted+=null, new EventArgs());

BTW this code (which is based on yours) uses moq not justmock but your exception is justmock related, I assume that using both causes methods and overloads confusions, just pick one and stick with it.

YuvShap
  • 3,825
  • 2
  • 10
  • 24