我有一个 CancelScheduledJob 方法,它接收后台作业的 Id 并使用 JobStorage 检索该 Id 并取消匹配的挂起作业:
var mon = JobStorage.Current.GetMonitoringApi();
var scheduledJobs = mon.ScheduledJobs(0, int.MaxValue);
var jobsToDelete = scheduledJobs.Where(job => job.Value.Job?.Args?.Any(arg => arg is Guid guid && guid == id) == true).ToList();
jobsToDelete?.ForEach(job => _backgroundJobClient.Delete(job.Key));
通过验证在 backgroundJobClient 模拟中调用的 Create 方法来验证 Enqueue() 或 Schedule() 方法,例如这里:
_backgroundJobClientMock.Verify(x => x.Create(
It.Is<Job>(job => job.Method.Name == "Run" && Guid.Parse(job.Args[0].ToString()) == input),
It.IsAny<ScheduledState>()));
但是我该如何验证 Delete 方法呢?我已经在模拟 JobStorage,但似乎找不到验证 Delete() 方法的方法。目前我有这个:
_backgroundJobClientMock.Verify(
x => x.Delete(It.Is<string>(jobId => jobId == "job1")),
Times.Once
);
但我遇到了一个常见问题,即 Delete 是一种扩展方法,不能在设置/验证表达式中使用。