David
David

Reputation: 19707

Mock a nested interface

I want to test that a method is called on the inner interface, how would I do such a thing with moq? Given the following example, I expect Z.Joy() to invoke Ix.Method().

interface Ix { void Method(); }
interface Iy<T> {}
class Z {
    public Z (Iy<Ix> y) {}
    public void Joy() {}
}

[TestClass]
public class Test {
    [TestMethod]
    public void ATest() {
        var x = new Mock<Ix>();
        var y = new Moxk<Iy<Ix>>(); // how can I pass x.Object?
        var z = new Z(y.Object);

        x.Verify(() => x.Method());

        z.Joy();
    }
}

Upvotes: 2

Views: 1522

Answers (1)

aqwert
aqwert

Reputation: 10789

Because you are mocking the interface Iy<T> you will have to place a expectation or setup on methods on that mocked interface that deal with Ix. I would expect you have something like this in Iy<T>.

interface Iy<T>
{
     T SomeMethod();
}

So here you can.

y.Setup(x => x.SomeMethod()).Returns(x.Object));

and somewhere in z.Joy() you will call x.SomeMethod()

Upvotes: 4

Related Questions