'How can I design this class to support mocking?
I'm trying to write unit tests with MOQ and I'm receiving this error message.
Invalid setup on a non-virtual (overridable in VB) member: x => x.Name
This makes sense since Name is defined in the IResource interface and I specified Resource in the mock setup.
ResourceGroupProperties does not belong to an interface. I was trying to design this will multiple interfaces instead of one huge one since not all Azure Resources have a location or tags.
Should I create a IResourceGroup interface and that defines the members not in one of the other 3 interfaces and then inherit from the 3 interfaces? Or I'm I over doing it with all the interfaces?
// Mock setup
var mockResourceGroup = new Mock<ResourceGroup>();
mockResourceGroup.Setup(x => x.Name).Returns(name);
mockResourceGroup.Setup(x => x.Properties.ProvisioningState).Returns("Provisioned");
// ResourceGroup class
public class ResourceGroup : IResource, IResourceTags, IResourceLocation
{
public string Etag { get; set; }
public string Id { get; set; }
public string Location { get; set; }
public string Name { get; set; }
public ResourceGroupProperties Properties { get; set; }
public IDictionary<string, string> Tags { get; set; }
public string Type { get; set; }
}
Solution 1:[1]
The class ResourceGroup has no functionality, just properties and no reason to use Moq. The following code is what I needed.
var mockResourceGroup = new ResourceGroup
{
Name = name,
Properties =
new ResourceGroupProperties
{
ProvisioningState = "Provisioned"
}
};
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jason |
