Reputation: 883
I have the following unit test which runs fine locally (simplified and anonymised):
[TestMethod]
public void GotoPageCommand_Calls_PCCollection_SearchPage()
{
var page = 100;
var sut = CreateSut();
sut.SearchPage = page;
sut.GotoPageCommand.Execute(null);
_tableViewModel.Verify(s => s.SearchPage(page), Times.Once());
}
The problem is the last line shows 0 times on the build server, but correctly shows 1 time when run locally (either on it's own or when running all tests).
The problem is obviously something to do with the async method.
This is a simplified version of the method being tested (the GotoPageCommand, which should then call the SearchPage method):
in the viewmodel:
public ICommand GotoPageCommand { get; }
. . .
// in the ctor:
GotoPageCommand = new DelegatingCommand(GotoPage, CanGotoPage);
. . .
// the GotoPage method
private async void GotoPage()
{
// lots of stuff...
try
{
await Task.Run(() =>
{
// This is the problem line that correctly verifies as being
// called once when run locally, but shows zero times when
// run on build server.
_pcCollection.PagData.SearchPage(page);
});
}
Upvotes: 0
Views: 46
Reputation: 4465
I think xUnit has support for testing async-void
, but if you cannot switch testing frameworks - there is an implementation that I wrote in another answer (check the others answers too) that would allow you to test ICommand.Execute
. For it to work, you'd need to make DelegatingCommand
's Execute
method async void
though.
Or you can do something very bad and add Thread.Sleep(20)
before the Verify
to simulate await
and work-around the race condition you have now.
Upvotes: 0