I’m trying to mock an method to a third-party library using moq.
The problem is that the method I’m mocking returns an object that is internal inside this framework, and thus I cannot instanciate this.
In the example below both the ChangeCollection and the ItemChange is internal, and I get the error: ‘Cannot access internal constructor ‘ChangeCollection’ here’
I’m having problems figuring out a good solution for this, does someone have any ideas?
[TestMethod]
public void GetItemsForExistingEMails_should_call_GetItems_atleast_once()
{
ewsMock = new Mock<IEwsIntegration>();
ewsMock.Setup(e => e.GetItems()).Returns(new ChangeCollection<ItemChange>);
var emailService = new EmailService(ewsMock.Object);
var items = emailService.GetItemsForExistingEMails();
ewsMock.Verify(e => e.GetItems(), Times.AtLeast(1));
Assert.AreEqual(0, items.Count());
}
public interface IEwsIntegration
{
ChangeCollection<ItemChange> GetItems();
}
I think you can get away with returning
IEnumerable<ChangeItem>in your interface. It seems like ChangeCollection is just an implementation ofIEnumerable<T>.Then you can simply return a list in your setup
Update:
Since you have to use properties defined only on the concrete class, you have to create an adapter.
First create an interface with the members from the concrete class.
Make sure you return this type from your interface:
Then you have to create an implementation of
IChangeCollection<T>which will simply direct calls to an instance of ChangeCollection.