简体   繁体   中英

How do I test a general exception in a unit test of my C# Console Application?

I'm curious as to how I can test against a general exception with unit tests, I am using MSTest for this and NSubstitute for mocking services.

This is a method from my C# Console application, it uploads a file and it's contents to Microsoft Azure. Logically all that exists is this method since I'm still in the unit testing phase but it makes the assumption the filename and the content will be passed to it when the time comes.

public async Task UploadBlob(string fileName, string content, bool overwrite = false)
{
    using MemoryStream stream = new(Encoding.UTF8.GetBytes(content));
    var blobClient = _container.GetBlobClient(fileName);

    try
    {
        // Upload to azure storage
        await blobClient.UploadAsync(stream, overwrite);
    }
    catch (Exception ex)
    {
        throw new Exception(ex.Message);
    }
}

You can see that we use a try/catch for the upload async portion of this method, for any number reasons this upload could fail and the exception would therefore be caught. I'm trying to mock this up in my unit test so that I can simulate this outcome but I am not sure how we can test this.

I've searched around and I couldn't find any resources with this sort of scenario, in my application I have more than once instance of a potential exception within a method, at the moment there is no code coverage for this and I'd like to have something in place that can simulate the failure.

I have started my unit test but I've hit a road block and not sure how to test

private readonly FileRepository _sut;
private readonly BlobServiceClient _bsc = Substitute.For<BlobServiceClient>();
private readonly BlobContainerClient _bcc = Substitute.For<BlobContainerClient>();
private readonly BlobClient _bc = Substitute.For<BlobClient>();

public FileRepositoryTests()
{
    _bsc.GetBlobContainerClient(default).ReturnsForAnyArgs(_bcc);
    _bcc.GetBlobClient(default).ReturnsForAnyArgs(_bc);
    _sut = new FileRepository(_blobServiceClient);
}

[TestMethod]
public async Task UploadBlob_FailesToUpload_ThrowsException()
{
    // Arrange
    var fileName = "2022-01-01file.txt";
    var fileContent = "Hello, world!";

    // Act
    ...This is just calling the service but I need to simulate the exception
    var result = _sut.UploadBlob(fileName, fileContent);

    // Assert
    ...Need to assert the exception but I have to mock it first
}

Any help is appreciated

You can wrap your call in a try catch block to get the exception. You can check the type of the exception and the error message to pass the test.

[TestMethod]
public async Task UploadBlob_FailesToUpload_ThrowsException()
{
    // Arrange
    var fileName = "2022-01-01file.txt";
    var fileContent = "Hello, world!";
    Exception expectedExcetpion = null;

    // Act
    try
    {
        var result = _sut.UploadBlob(fileName, fileContent);
    }
    catch (Exception ex)
    {
        expectedExcetpion = ex;
    }

    // Assert
    Assert.IsNotNull(expectedExcetpion);

}

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