My code is the following:
SubscriptionResource subscription = await armClient.GetDefaultSubscriptionAsync();
var resourceGroup = subscription.GetResourceGroups().FirstOrDefault(rg => rg.Data.Name.Equals(resourceGroupName));
GetResourceGroups() returns an instance of ResourceGroupCollection. When the ResourceGroupCollection is enumerated by SingleOrDefault, I'm trying to get the mock to return a mock instance of a ResourceGroupResource. However, I'm not sure how to do that.
So far I have this code in my Moq test:
this.resourceGroupCollectionMock = new Mock<ResourceGroupCollection>() { CallBase = true };
Mock<ResourceGroupResource> adfResource = new Mock<ResourceGroupResource>();
ResourceGroupData rgData = ResourceManagerModelFactory.ResourceGroupData(resourceIdentifier, resourceGroupName);
adfResource.Setup(x => x.Data).Returns(rgData);
this.subscriptionResourceMock.Setup(x => x.GetResourceGroups()).Returns(this.resourceGroupCollectionMock.Object);
As you can see in my setup, I mocked GetResourceGroups() to return the mock ResourceGroupCollection object. But I'm not sure how to add adfResource to that mock resourceGroupCollectionMock, so that it is returned when the latter is enumerated.
TL;DR: Unfortunately you can't mock
ResourceGroupDataproperly becauseResourceData'sName(1, 2) is not defined asvirtual/abstract;(So, if you don't use the
Nameproperty in your predicate (like in the above sample) then you can mock/fake the rest of the classes.