Open jeremysimmons opened 2 months ago
Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @anthony-c-martin @calecarter @cemheren @j5lim @majastrz.
Hi @jeremysimmons, Thank you for your feedback. I have written a mock test for CostManagementExportCollection that works. After running it locally, it successfully executed. I hope this piece of code can provide you with some assistance.
using System;
using System.Threading.Tasks;
using Azure.Core;
using Azure.ResourceManager.CostManagement.Mocking;
using Azure.ResourceManager.CostManagement.Models;
using Azure.ResourceManager.Resources;
using Moq;
using NUnit.Framework;
namespace Azure.ResourceManager.CostManagement.Tests
{
public class MockExportResource
{
[Test]
public async Task Mocking_GetCollectionAndCreate()
{
#region mocking data
var subscriptionId = Guid.NewGuid().ToString();
var resourceGroupName = "myRg";
var exportName = "myExport";
var exportScope = $"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}";
var exportId = CostManagementExportResource.CreateResourceIdentifier(exportScope, "exportName");
var exportData = ArmCostManagementModelFactory.CostManagementExportData(exportId, exportName);
#endregion
#region mocking setup
var clientMock = new Mock<ArmClient>();
var clientExtensionMock = new Mock<MockableCostManagementArmClient>();
var rgMock = new Mock<ResourceGroupResource>();
//for CostManagementExport
var exportCollectionMock = new Mock<CostManagementExportCollection>();
var exportMock = new Mock<CostManagementExportResource>();
var exportLroMock = new Mock<ArmOperation<CostManagementExportResource>>();
//set some data in the result
exportMock.Setup(exportMock => exportMock.Id).Returns(exportId);
exportMock.Setup(exportMock => exportMock.Data).Returns(exportData);
// first mock: mock the same method in mocking extension class
clientExtensionMock.Setup(e => e.GetCostManagementExports(new ResourceIdentifier(exportScope))).Returns(exportCollectionMock.Object);
// second mock: mock the GetCachedClient method on the "extendee"
clientMock.Setup(rg => rg.GetCachedClient(It.IsAny<Func<ArmClient, MockableCostManagementArmClient>>())).Returns(clientExtensionMock.Object);
// setup the mock on the collection for CreateOrUpdate method
exportCollectionMock.Setup(c => c.CreateOrUpdateAsync(WaitUntil.Completed, exportName, exportData, default)).ReturnsAsync(exportLroMock.Object);
exportLroMock.Setup(lro => lro.Value).Returns(exportMock.Object);
#endregion
//the mocking test
var client = clientMock.Object;
var exportCollection = client.GetCostManagementExports(new ResourceIdentifier(exportScope));
var exportlro = await exportCollection.CreateOrUpdateAsync(WaitUntil.Completed, exportName, exportData, default);
var export = exportlro.Value;
Assert.AreEqual(exportId, export.Id);
Assert.AreEqual(exportName, export.Data.Name);
Assert.AreEqual(exportData, export.Data);
}
}
}
Hi @jeremysimmons. Thank you for opening this issue and giving us the opportunity to assist. To help our team better understand your issue and the details of your scenario please provide a response to the question asked above or the information requested above. This will help us more accurately address your issue.
Library name and version
Azure.ResourceManager.CostManagement 1.0.1
Query/Question
It is quite difficult to mock functionality related to the Cost Management api, specifically around the CostManagementExportResource.
Once you've instantiated the
According to the design guidelines, https://azure.github.io/azure-sdk/dotnet_introduction.html,
I'm assuming this is because ArmClient is in Azure.ResourceManager and CostManagementExtensions is in Azure.ResourceManager.CostManagement.
I would much rather have a CostManagementClient that takes an ArmClient as a parameter than have the current debacle.
Additionally this guidance seems to be missing completely.
Maybe I'm missing it, but I wanted a simple/easy way for my business class to call. If I'm missing something obvious, please let me know.
Environment
dotnet core 6 VS 2022 v17.9.6