简体   繁体   中英

How to unit test aync Task without type

I have async methods which doesn't have any return type , it just has Task type.

How can I write valid unit test method for just Task ?

Async Methods:

public async Task UploadFileAsync(FileDetails data)
{

   await _fileUpload.UploadImageAsync(data);

}


public async Task UploadImageAsync(FileDetails data)
{  

// does a async operation to upload a stream to azure blob storage

await azureBlob.UploadFromStreamAsync(streamData).ConfigureAwait(false)

}

Test Method:

[TestMethod]
public async Task UploadFileAsync()
{
    //-->Arrange

    _fileUpload.Setup(e => e.UploadImageAsync().Returns(new Task<Task>(() => null));

    //-->Act

    Task result = _uploadManager.UploadFileAsync(GetFakeUploadData());

    // Task doesn't return any result ?

    //-->Assert


}

Update:

Finally I call this method to upload file to azure blob storage.

You can just change your Act section to:

//-->Arrange

_fileUpload
    .Setup(e => e.UploadImageAsync(It.IsAny<FileDetails>()))
    .Returns(Task.FromResult<object>((object)null))
    .Verifiable(); //<-- Allows you to verify that the mock was invoked correctly

//Act
await _uploadManager.UploadFileAsync(GetFakeUploadData());

//Assert
_fileUpload.Verify();

Reference Moq Quickstart to get a better understanding of how to use the Moq framework.

An async method returning Task is similar to a regular void method. You can await the task to ensure that the method runs before your test completes:

Task result = _uploadManager.UploadFileAsync(GetFakeUploadData());
await result.ConfigureAwait(false);

There is no assertion to make here - all you could say about the method is that it runs without triggering an error.

If the server returns some token to your application when upload is complete (say, some proprietary resource identifier) than it would be beneficial to change the signature of UploadFileAsync to make that identifier available to the caller:

public async Task<UploadedResourceIdentifier> UploadFileAsync(FileDetails data) {
    var ret = await _fileUpload.UploadImageAsync(data).ConfigureAwait(false);
    return new UploadedResourceIdentifier(ret); // Parses the return, and constructs resource identifier
}

Now the test would have something to assert:

var result = await _uploadManager
    .UploadFileAsync(GetFakeUploadData())
    .ConfigureAwait(false);
Assert.That(result.IsValid, Is.True);

我建议使用ContinueWith方法,以便您可以检查IsFaulted成员。

result.ContinueWith(t => Assert.IsFalse(t.IsFaulted));
await _uploadManager.UploadFileAsync(GetFakeUploadData());

to execute the operation and wait for it to complete, and then you should somehow access the uploaded file and verify it's there. If you can upload you can probably also download, so use that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM