我正在使用NUnit和Moq来测试一些简单的类。
当我运行测试时,它在第二次起订量验证检查中失败。奇怪的是,我在相似的课程中做了完全相同的事情,而且效果很好。我一直在寻找类似的问题,但似乎只能找到返回异步任务的答案。
如果我交换正在实际类中测试的方法调用的顺序,以便首先调用PushAsync方法,则PostStats验证失败。 这会让我认为它只能执行一个验证,而在其他测试中我都做过两次(都具有相同的返回类型)。
我得到的错误是:
Message: Moq.MockException :
Expected invocation on the mock at least once, but was never performed: p => p.PushAsync(It.IsAny<ExerciseNotes>())
Configured setups:
IPageService p => p.PushAsync(It.IsAny<ExerciseNotes>())
No invocations performed
这是我的课程:
public class ExerciseRatingViewModel
{
public int DifficultyRating { get; set; }
public int PainRating { get; set; }
public int MobilityRating { get; set; }
public ICommand SubmitRatingsCommand { get; }
private readonly IPageService _pageService;
private readonly IDataService _dataService;
private readonly IAPIService _apiService;
private int _sequenceID;
public ExerciseRatingViewModel(int sequenceID, IPageService pageService, IDataService dataService, IAPIService apiService)
{
_sequenceID = sequenceID;
_pageService = pageService;
_dataService = dataService;
_apiService = apiService;
SubmitRatingsCommand = new Command(SubmitStats);
}
private async void SubmitStats()
{
int userID = _dataService.GetUserID();
SequenceRating sequenceRating = new SequenceRating(_sequenceID, userID, DifficultyRating, PainRating, MobilityRating);
bool success = await _apiService.PostStats(sequenceRating);
await _pageService.PushAsync(new ExerciseNotes());
}
}
这是我的考试班
class ExerciseRatingViewModelTests
{
private ExerciseRatingViewModel _exerciseRatingViewModel;
private Mock<IPageService> _pageService;
private Mock<IDataService> _dataService;
private Mock<IAPIService> _apiService;
[SetUp]
public void Setup()
{
_pageService = new Mock<IPageService>();
_dataService = new Mock<IDataService>();
_apiService = new Mock<IAPIService>();
int sequenceID = 1;
_exerciseRatingViewModel = new ExerciseRatingViewModel(sequenceID, _pageService.Object, _dataService.Object, _apiService.Object);
}
[Test()]
public void SubmitStats_WhenTouched_ShouldNavigateToNotesPage()
{
//Arrange
_apiService.Setup(a => a.PostStats(It.IsAny<SequenceRating>())).ReturnsAsync(true);
_pageService.Setup(p => p.PushAsync(It.IsAny<ExerciseNotes>()));
// Act
_exerciseRatingViewModel.SubmitRatingsCommand.Execute(null);
//Assert
_apiService.Verify(a => a.PostStats(It.IsAny<SequenceRating>()));
_pageService.Verify(p => p.PushAsync(It.IsAny<ExerciseNotes>()));
}
}
PostStats方法的签名为:
Task<bool> PostStats(SequenceRating sequenceRating);
PushAsync方法的签名是:
Task PushAsync(Page page);
答案 0 :(得分:1)
第一个重构视图模型避免使用async void
函数,但实际的事件处理程序除外
public ExerciseRatingViewModel(int sequenceID, IPageService pageService, IDataService dataService, IAPIService apiService) {
_sequenceID = sequenceID;
_pageService = pageService;
_dataService = dataService;
_apiService = apiService;
submitted += onSubmitted; //subscribe to event
SubmitRatingsCommand = new Command(() => submitted(null, EventArgs.Empty));
}
private event EventHandler submitted = delegate { };
private async void onSubmitted(object sender, EventArgs args) { //event handler
await SubmitStats();
}
private async Task SubmitStats() {
int userID = _dataService.GetUserID();
SequenceRating sequenceRating = new SequenceRating(_sequenceID, userID, DifficultyRating, PainRating, MobilityRating);
bool success = await _apiService.PostStats(sequenceRating);
await _pageService.PushAsync(new ExerciseNotes());
}
引用Async/Await - Best Practices in Asynchronous Programming
您正在使用Task
,因此测试必须是异步的,并且模拟程序还需要返回Task
以允许异步按预期方式流动。您已经为PostStats
做过。现在只需对PushAsync
做同样的事情。
[Test()]
public async Task SubmitStats_WhenTouched_ShouldNavigateToNotesPage() {
//Arrange
var tcs = new TaskCompletionSource<object>();
_apiService.Setup(a => a.PostStats(It.IsAny<SequenceRating>())).ReturnsAsync(true);
_pageService.Setup(p => p.PushAsync(It.IsAny<Page>()))
.Callback((Page arg) => tcs.SetResult(null))
.Returns(Task.FromResult((object)null));
// Act
_exerciseRatingViewModel.SubmitRatingsCommand.Execute(null);
await tcs.Task; //wait for async flow to complete
//Assert
_apiService.Verify(a => a.PostStats(It.IsAny<SequenceRating>()));
_pageService.Verify(p => p.PushAsync(It.IsAny<ExerciseNotes>()));
}
在测试中,在模拟的回调中使用TaskCompletionSource
,以便允许在尝试验证行为之前等待异步代码完成。这样做是因为事件和命令在不同的线程上执行。