Azure / azure-sdk-for-net

This repository is for active development of the Azure SDK for .NET. For consumers of the SDK we recommend visiting our public developer docs at https://learn.microsoft.com/dotnet/azure/ or our versioned developer docs at https://azure.github.io/azure-sdk-for-net.
MIT License
5.57k stars 4.83k forks source link

[QUERY] Mocking ArmDeploymentData #33895

Open adstep opened 1 year ago

adstep commented 1 year ago

Library name and version

Azure.ResourceManager.Resources 1.3.1

Query/Question

I'm trying to write some tests interacting with an ArmDeploymentResource. I'm having trouble understanding how to properly mock an ArmDeploymentData. The Data property is virtual, which is great, but I can't spin up a mocked version of ArmDeploymentData or ArmDeploymentPropertiesExtended because their constructors are internal and their properties are not virtual.

For example the following:

new Mock<ArmDeploymentData>()
    .SetupGet(o => o.Properties)
    .Returns(new Mock<ArmDeploymentPropertiesExtended>().Object);

Will fail with:

System.NotSupportedException : Unsupported expression: o => o.Properties
Non-overridable members (here: ArmDeploymentData.get_Properties) may not be used in setup / verification expressions.

The .NET Azure SDK Guidelines states:

DO provide factory or builder for constructing model graphs returned from virtual service methods.

Is there a factory method for instantiating ArmDeploymentData/ArmDeploymentResource?

Environment

.NET SDK: Version: 7.0.102 Commit: 4bbdd14480

Runtime Environment: OS Name: Windows OS Version: 10.0.25290 OS Platform: Windows RID: win10-x64 Base Path: C:\Program Files\dotnet\sdk\7.0.102\

Host: Version: 7.0.2 Architecture: x64 Commit: d037e070eb

.NET SDKs installed: 3.1.426 [C:\Program Files\dotnet\sdk] 5.0.416 [C:\Program Files\dotnet\sdk] 6.0.113 [C:\Program Files\dotnet\sdk] 7.0.102 [C:\Program Files\dotnet\sdk]

.NET runtimes installed: Microsoft.AspNetCore.All 2.1.30 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.App 2.1.30 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.1.32 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 6.0.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 7.0.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 2.1.30 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.1.32 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 6.0.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 7.0.2 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.WindowsDesktop.App 3.1.32 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 6.0.13 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 7.0.2 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other architectures found: x86 [C:\Program Files (x86)\dotnet] registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]

Environment variables: Not set

global.json file: C:\dev\github\autorest.csharp\global.json

Learn more: https://aka.ms/dotnet/info

Download .NET: https://aka.ms/dotnet/download

jsquire commented 1 year ago

Thank you for your feedback. Tagging and routing to the team member best able to assist.

fengzhou-msft commented 1 year ago

@ArcturusZhang is working on a PR to provide model factories: https://github.com/Azure/azure-sdk-for-net/pull/32867