Piotr
Piotr

Reputation: 185

Moq, how verifing method from interface inside another interface

I have 2 interfaces Interface A has one method

InterfaceB CreateObjectOfInterfaceB();

And Interface B has method

void DoSth();

How can i verify if method CreateObjectOfInterfaceB().DoSth() was used?

Upvotes: 2

Views: 136

Answers (1)

Lucas Trzesniewski
Lucas Trzesniewski

Reputation: 51330

You have two objects, so you'll need two mocks. Setup your first mock to return the second one, and it should be straightforward from there.

var mockA = new Mock<InterfaceA>();
var mockB = new Mock<InterfaceB>();

mockA.Setup(i => i.CreateObjectOfInterfaceB()).Returns(mockB.Object);

// Do your test

mockA.Verify(i => i.CreateObjectOfInterfaceB(), Times.Once);
mockB.Verify(i => i.DoSth(), Times.Once);

Upvotes: 2

Related Questions