question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[QUERY] - How do I mock an IPage<DeploymentOperation> for my unit tests

See original GitHub issue

Query/Question I saw this blog post which provides an example of making a Page using the .FromValues method but with the Azure Resource Manager SDK, I don’t see this exposed as an option when trying to mock a DeploymentOperations object.

We are specifically trying to setup a moq for this method:

image

ResourceManagerClient.DeploymentOperations.ListAtScopeWithHttpMessagesAsync

This method returns an object of type

Task<AzureOperationResponse<IPage<DeploymentOperation>>>

We have tried a number of things and been unable to create a Mock object of this type for unit testing. How do we convert our test response object into a Paged version? Note: making an AzureOperationResponce isn’t too hard, it’s the Page/IPage that is giving us grief.

var operationProperties = new DeploymentOperationProperties(
                provisioningOperation: ProvisioningOperation.Create,
                provisioningState:"complete",
                DateTime.UtcNow,
                duration:"2 minutes", 
                serviceRequestId:null, 
                statusCode: "OK", 
                statusMessage: new StatusMessage("OK"));

            var myOperation = new DeploymentOperation(
                id:"someid123", operationId:"hello",
                properties: operationProperties);

            var l = myOperation as Page<DeploymentOperation>;

This gives an error of:

Cannot convert type ‘Microsoft.Azure.Management.ResourceManager.Models.DeploymentOperation’ to ‘Microsoft.Azure.Management.ResourceManager.Models.Page<Microsoft.Azure.Management.ResourceManager.Models.DeploymentOperation>’ via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion

Documentation for this shows people using Page<T>.FromValues which we do not see as available.

Environment:

  • Name and version of the Library package used:
    Microsoft.Azure.Management.ResourceManager. 3.13.1-preview
  • Hosting platform or OS and .NET runtime version (dotnet --info output for .NET Core projects): [e.g. Azure AppService or Windows 10 .NET Framework 4.8]
  • IDE and version : [e.g. Visual Studio 16.3]

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
pakrymcommented, Sep 7, 2021

The easiest might be to compy one of the existing page models that implement IPage and use it in tests:

https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/redis/Microsoft.Azure.Management.Redis/src/Generated/Models/Page.cs

var operationProperties = new DeploymentOperationProperties(
                provisioningOperation: ProvisioningOperation.Create,
                provisioningState:"complete",
                DateTime.UtcNow,
                duration:"2 minutes", 
                serviceRequestId:null, 
                statusCode: "OK", 
                statusMessage: new StatusMessage("OK"));

            var myOperation = new DeploymentOperation(
                id:"someid123", operationId:"hello",
                properties: operationProperties);
var page = new Page<DeploymentOperation>()
{
    Items = new List<DeploymentOperation>()
	{
		{  myOperation  }
	}
}
0reactions
msftbot[bot]commented, Sep 15, 2021

Hi @1RedOne, since you haven’t asked that we “/unresolve” the issue, we’ll close this out. If you believe further discussion is needed, please add a comment “/unresolve” to reopen the issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mocking AWS with Jest (and TypeScript)
We use unit tests and Jest mocks to write the code, then deploy to a developer-namespaced stack in our development account for integration ......
Read more >
Stubbing and Mocking in Java with the Spock Testing ...
Learn how to create true Java unit tests by mocking all external dependencies in your unit tests with the Spock testing framework.
Read more >
Mocking with examples
Creating a mock server. Step 1: Send a request; Step 2: Save the request to a collection; Step 3: Save a response as...
Read more >
Build with Confidence: A Guide to JUnit Tests
Let's jump into Java unit testing in Spring using the JUnit framework. We'll start with something you may have heard of: mocking. What...
Read more >
Unit Test - Creating Custom Metadata
For this hack you just need to put query for custom metadata into separate property which should be settable for unit tests.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found