gpt4 book ai didi

c# - 验证从异步调用报告的进度是否在单元测试中正确报告

转载 作者:行者123 更新时间:2023-12-03 10:35:30 24 4
gpt4 key购买 nike

我正在研究一些可以自动检测设备(在本例中为光谱仪)连接到的串行端口的代码。

我有自动检测部分工作,我正在尝试为 ViewModel 编写测试,以显示检测、错误等的进度。

这是执行实际检测的代码的接口(interface)。

public interface IAutoDetector
{
Task DetectSpectrometerAsync(IProgress<int> progress);
bool IsConnected { get; set; }
string SelectedSerialPort { get; set; }
}

这是使用 IAutoDetector 的 ViewModel检测光谱仪
public class AutoDetectViewModel : Screen
{
private IAutoDetector autoDetect;
private int autoDetectionProgress;

public int AutoDetectionProgress
{
get { return autoDetectionProgress; }
private set
{
autoDetectionProgress = value;
NotifyOfPropertyChange();
}
}

[ImportingConstructor]
public AutoDetectViewModel(IAutoDetector autoDetect)
{
this.autoDetect = autoDetect;
}

public async Task AutoDetectSpectrometer()
{
Progress<int> progressReporter = new Progress<int>(ProgressReported);
await autoDetect.DetectSpectrometerAsync(progressReporter);
}

private void ProgressReported(int progress)
{
AutoDetectionProgress = progress;
}
}

我正在尝试编写一个测试来验证 IAutoDetector 报告的进度。更新 AutoDetectionProgress AutoDetectionViewModel 中的属性(property).

这是我当前的(非工作)测试:
[TestMethod]
public async Task DetectingSpectrometerUpdatesTheProgress()
{
Mock<IAutoDetector> autoDetectMock = new Mock<IAutoDetector>();
AutoDetectViewModel viewModel = new AutoDetectViewModel(autoDetectMock.Object);

IProgress<int> progressReporter = null;
autoDetectMock.Setup(s => s.DetectSpectrometerAsync(It.IsAny<IProgress<int>>()))
.Callback((prog) => { progressReporter = prog; });

await viewModel.AutoDetectSpectrometer();
progressReporter.Report(10);
Assert.AreEqual(10, viewModel.AutoDetectionProgress);
}

我想做的是捕获 IProgress<T>传递给 autoDetect.DetectSpectrometerAsync(progressReporter) ,告诉 IProgress<T>报告进度为 10,然后确保 AutoDetectionProgressviewModel也是10。

然而,这段代码有两个问题:
  • 它不编译。 autoDetectMock.Setup行有错误:Error 1 Delegate 'System.Action' does not take 1 arguments .我在其他(非同步)测试中使用了相同的技术来访问传递的值。
  • 这种方法甚至会奏效吗?如果我对异步的理解是正确的,调用 await viewModel.AutoDetectSpectrometer();在调用 progressReporter.Report(10); 之前将等待调用完成,这不会有任何影响,因为 AutoDetectSpectrometer()电话已经返回。
  • 最佳答案

    您必须指定回调的返回类型;编译器将无法为您确定这一点。

    autoDetectMock
    .Setup(s => s.DetectSpectrometerAsync(It.IsAny<IProgress<int>>()))
    .Callback((prog) => { progressReporter = prog; }); // what you have

    应该
    autoDetectMock
    .Setup(s => s.DetectSpectrometerAsync(It.IsAny<IProgress<int>>()))
    .Callback<IProgress<int>>((prog) => { progressReporter = prog; });

    您也没有从设置中返回任务,因此也会失败。您需要返回一个任务。

    我相信,在你解决这两个问题之后,它应该可以工作。

    关于c# - 验证从异步调用报告的进度是否在单元测试中正确报告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31320444/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com