lundi 4 février 2019

How to test the status of a an object at a specific state?

I have a ReportHandler class, which can produce a report with 3 different statuses.

  • InProgress, when the report is being generated (this could take from seconds to minutes)
  • Success, when everything is OK
  • Error, when something has gone wrong

Inside the handler class, there is a mocked report generator, which setting it up accordingly, to return a report or throw an exception, I can test those scenarios.

My problem is, that I need to write a test for the report which is in 'InProgress' status.

public async Task WriteReportAsync(SearchTermsRequestData requestData, string correlationId, CancellationToken cancellationToken)
    {
        WriteToFile(new Report { Status = ReportStatusType.InProgress.ToString() }, correlationId, _applicationSettings.OutputPath);
        var reportGenerator = _reportGeneratorFactory.GetReportGenerator(requestData);

        try
        {
            // reportGenerator is mocked in tests
            // once is mocked to return a report = success
            // once is mocked to throw exeption = error
            var report = await reportGenerator
                .Generate(requestData);
            report.Status = ReportStatusType.Success.ToString();
            WriteToFile(report, correlationId, _applicationSettings.OutputPath);
        }
        catch (Exception exception)
        {
            WriteToFile(new Report { Status = ReportStatusType.Error.ToString(), Error = exception.Message }
                , correlationId, _applicationSettings.OutputPath);
            _logger.Error(exception.Message);
        }
    }

How could i write a test, which can check the InProgress status which the report is written with as soon as the WriteReportAsync method is called?

I have thought to write a moq with delay, but that delays the the overall execution and the assertions arent hit until the delay has passed, so that doesnt work.

Aucun commentaire:

Enregistrer un commentaire