Repository: Azure/azure-sdk-for-go

Issue #16613: Fakes Support
Created: 2021-12-10T14:36:50Z
I'm wondering if there will be  support for mock client because it will help end user develop unit test case.

Duplicate issue #20695: Mocking the Poller<T> for armcompute results in an error
Created: 2023-04-25T17:11:15Z
Bug Report
I am using latest "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute"

What happened?
I created an AzureClient and now I am trying to implement the MockClient for the same in /services module I was easily able to do so using Futures and returning after shifting to the arm module I am facing type assertion errors.

I tried creating an interface for my usage like
type ComputeVirtualMachinesClient interface {
	BeginStart(ctx context.Context, resourceGroupName string, vmName string, options *armcompute.VirtualMachinesClientBeginStartOptions) (*runtime.Poller[armcompute.VirtualMachinesClientStartResponse], error)
	BeginRestart(ctx context.Context, resourceGroupName string, vmName string, options *armcompute.VirtualMachinesClientBeginRestartOptions) (*runtime.Poller[armcompute.VirtualMachinesClientRestartResponse], error)
	BeginDelete(ctx context.Context, resourceGroupName string, vmName string, options *armcompute.VirtualMachinesClientBeginDeleteOptions) (*runtime.Poller[armcompute.VirtualMachinesClientDeleteResponse], error)
	BeginDeallocate(ctx context.Context, resourceGroupName string, vmName string, options *armcompute.VirtualMachinesClientBeginDeallocateOptions) (*runtime.Poller[armcompute.VirtualMachinesClientDeallocateResponse], error)
	BeginCreateOrUpdate(ctx context.Context, resourceGroupName string, vmName string, parameters armcompute.VirtualMachine, options *armcompute.VirtualMachinesClientBeginCreateOrUpdateOptions) (*runtime.Poller[armcompute.VirtualMachinesClientCreateOrUpdateResponse], error)
	NewListAllPager(options *armcompute.VirtualMachinesClientListAllOptions) *runtime.Pager[armcompute.VirtualMachinesClientListAllResponse]
	NewListPager(resourceGroupName string, options *armcompute.VirtualMachinesClientListOptions) *runtime.Pager[armcompute.VirtualMachinesClientListResponse]
	Get(ctx context.Context, resourceGroupName string, vmName string, options *armcompute.VirtualMachinesClientGetOptions) (armcompute.VirtualMachinesClientGetResponse, error)
	BeginPowerOff(ctx context.Context, resourceGroupName string, vmName string, options *armcompute.VirtualMachinesClientBeginPowerOffOptions) (*runtime.Poller[armcompute.VirtualMachinesClientPowerOffResponse], error)
	InstanceView(ctx context.Context, resourceGroupName string, vmName string, options *armcompute.VirtualMachinesClientInstanceViewOptions) (armcompute.VirtualMachinesClientInstanceViewResponse, error)
}

and now when I am trying to mock this same using the mock poller I get this error
type mockPoller[T any] interface {
	PollUntilDone(ctx context.Context, options *runtime.PollUntilDoneOptions) (T, error)
	Poll(ctx context.Context) (*http.Response, error)
	Done() bool
	Result(ctx context.Context) (T, error)
	ResumeToken() (string, error)
}

./azure_client.go:502:9: cannot use virtualMachineClient (variable of type *armcompute.VirtualMachinesClient) as ComputeVirtualMachinesClient value in return statement: *armcompute.VirtualMachinesClient does not implement ComputeVirtualMachinesClient (wrong type for method BeginStart)
                have BeginStart(context.Context, string, string, *armcompute.VirtualMachinesClientBeginStartOptions) (*"github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime".Poller[armcompute.VirtualMachinesClientStartResponse], error)
                want BeginStart(context.Context, string, string, *armcompute.VirtualMachinesClientBeginStartOptions) (mockPoller[armcompute.VirtualMachinesClientStartResponse], error)


What did you expect or want to happen?
I should be able to write the unit test cases for the project I am working on.
Can someone suggest me good examples where I can see how to do that?

Closing comment (2023-04-27T15:30:05Z): Your mock methods will need to return an interface instead of a Poller[T].  You can find an example here.
Longer term, our current plan is to generate a fake package per module that lets one wire up a real client to a fake endpoint, no interfaces required.  That work is tracked in #16613 and we hope to have a beta out in the next release cycle (May).