I have a Prism dialogService for confirmation (Yes and No buttons in dialog), and I use it this way;
bool confirmed = false;
DialogParameters p = new DialogParameters
{
{ "title", title },
{ "message", message }
};
dialogService.ShowDialog("ConfirmationDialog", p, r => { confirmed = r.Result == ButtonResult.OK; });
if (!confirmed)
{
return;
}
// code I am not able to test
It works like a charm, but I am not able to test the code after the return, because "confirmed" is always false while I am executing tests, so it always returns and the code after the "return" is never executed.
I think the best way to test it should be to inject somehow the IDialogResult value in "r", so I can force that always I am testing r.Result == ButtonResult.OK. But... how can I do that?
Maybe my approach for confirmation dialog is not good, or maybe I am missing something, so any suggestion would be a great help for me.
Note: I use Fake It Easy for testing, but it is not a problem is the solution in explained in any other testing framework.
UPDATE: this is the implementation of the dialogService in the test:
dialogService = A.Fake<IDialogService>();
And then it is injected in the ViewModel:
var viewModel = new MyViewModel(dialogService);
Thank you
dialogServicein your test. If it's a mock, you could tell it to pass to the callback any arguments you desire... - Haukinger