1

I have converted to my code from Nsubstitute to Moq.

Nsubstitute:

var item = Substitute.For<Item>(id, itemData, database); 
item.Axes.Returns(Substitute.For<ItemAxes>(item)); 
return item; 

Moq:

var item = new Mock<Item>(id, itemData, database); 
item.Axes.Returns(new Mock<ItemAxes>(item)); 
return item; 

I am getting an error in the Moq sample code.

2

Of course, it is possible. Moq has a different API, so I'd suggest to start from this guide first.

This code sample configures item descendants to return an empty array:

var itemMock = new Mock<Item>(id, itemData, database);
var axesMock = new Mock<ItemAxes>(itemMock.Object);
var expectedDescendants = new Item[] { };

itemMock.Setup(i => i.Axes).Returns(axesMock.Object);
axesMock.Setup(a => a.GetDescendants()).Returns(expectedDescendants);

var actualDescendants = itemMock.Object.Axes.GetDescendants();

Assert.Same(expectedDescendants, actualDescendants);
1
  • Thanks much for your detailed answer. I fixed my code.
    – BalaSM
    Jun 4 '20 at 14:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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