devlooped / moq

The most popular and friendly mocking framework for .NET
Other
5.96k stars 801 forks source link

Mocking Blobclient.DownloadContentAsync() #1411

Open sarwary opened 1 year ago

sarwary commented 1 year ago

Hello, am I mocking the method below correctly coz, when called its not returning the result:

var blobContent = new BinaryData("this is test data");
var downloadResult = BlobsModelFactory.BlobDownloadResult(content: blobContent);
var response = Response.FromValue(downloadResult, blobContainerClientResponseMock.Object);

mockBlobClientTrue.Setup(client => client.DownloadContentAsync(default)).ReturnsAsync(response);

the reslut object of the task returned form this method is null.

Back this issue Back this issue

alexdtm9 commented 1 year ago

I am using it like this in my code and it works:

var response = await blobClient.DownloadContentAsync();
if (!response.HasValue || response.GetRawResponse().IsError) (....)

This is how I mocked:

 var mockResponse = new Mock<Response>();
 var mockValue = BlobsModelFactory.BlobDownloadResult(new BinaryData("some data"), default);
_blobClient.Setup(x => x.DownloadContentAsync(default)).ReturnsAsync(Response.FromValue(mockValue, mockResponse.Object));

If I pass 'default' or 'It.IsAny()' inside my mocking the result is always null.

anktsrkr commented 1 year ago

In this blog post i have exactly defined how to do - https://anktsrkr.github.io/post/getting-started-with-testing-for-azure-blob-storage-mocking-blob-storage-sdk/

anktsrkr commented 1 year ago

In this blog post i have exactly defined how to do - https://anktsrkr.github.io/post/getting-started-with-testing-for-azure-blob-storage-mocking-blob-storage-sdk/

Now it is updated to FakeItEasy. However if you follow the repo and see the old commits you will still be able to find which is using MOQ

github-actions[bot] commented 3 months ago

Due to lack of recent activity, this issue has been labeled as 'stale'. It will be closed if no further activity occurs within 30 more days. Any new comment will remove the label.

kzu commented 2 months ago

Easiest way to detect whether you're missing a setup (or the compiler is not picking the overload you think it should be picking) is to set the mock to MockBehavior.Strict and see where it throws. That will tell you which setup is missing.