15

I'm trying to unit test the LoginExecute method of the following ViewModel using MOQ

public class LoginViewModel : ViewModelBase, ILoginViewModel
{
    INavigationService navigationService;
    IDialogService dialogService;
    IAdminService adminService;

    public RelayCommand LoginCommand { get; set; }

    private string _productID;

    public string ProductID
    {
        get { return _productID; }
        set
        {
            _productID = value;
            RaisePropertyChanged("ProductID");
        }
    }

    public LoginViewModel(INavigationService navigationService, IDialogService dialogService, IAdminService adminService)
    {
        this.navigationService = navigationService;
        this.dialogService = dialogService;
        this.adminService = adminService;
        InitializeCommands();
    }

    private void InitializeCommands()
    {
        LoginCommand = new RelayCommand(() => LoginExecute());
    }

    public async Task LoginExecute()
    {
        await this.navigationService.TestMethod();
        this.navigationService.Navigate(typeof(ITherapistsViewModel));
    }

    public void Initialize(object parameter)
    {
    }
}

The INavigationService looks like this

    public interface INavigationService
{
    Frame Frame { get; set; }
    void Navigate(Type type);
    void Navigate(Type type, object parameter);
    Task TestMethod();
    void GoBack();
}

My test looks like this

[TestMethod()]
    public async Task LoginCommandTest()
    {
        var navigationService = new Mock<INavigationService>();
        var dialogService = new Mock<IDialogService>();
        var adminService = new Mock<IAdminService>();
        LoginViewModel loginVM = new LoginViewModel(navigationService.Object, dialogService.Object, adminService.Object);

        await loginVM.LoginExecute();

        //Asserts will be here
    }

The problem is that when line

await this.navigationService.TestMethod();

is called the NullReferenceException is being thrown. If the same method is called without "await" it works as expected. It also works ok if the method is being called on normal NavigationService implementation (not a mock of it). Could you please help me understand why the async method call is producing NullReferenceException?

2 Answers 2

20

Part of the Task-based Asynchronous Pattern is an implicit assumption that the method never returns null.

This means that for all asynchronous methods, you'll need to mock an actual return value. I recommend using Task.FromResult for the "synchronous success" case, TaskCompletionSource for the "synchronous exception" case, and an async lambda with Task.Delay for the "asynchronous success/exception" cases.

0
19

I had something similar to this; I had a setup with a callback on an async Task, with the code like this:

var navigationService = new Mock<INavigationService>()
    .Setup(s => s.TestMethod())
    .Callback(...);

And it threw an obscure NullReferenceException; adding a Returns(Task.CompletedTask) fixed the issue:

var navigationService = new Mock<INavigationService>()
   .Setup(s => s.TestMethod())
   .Returns(Task.CompletedTask)
   .Callback(...);

Got caught by this today, hope that will help someone with a similar issue.

1
  • 3
    This exact issue, this morning, kept my pulling my hair out for hours.
    – Dark Hippo
    Oct 9, 2017 at 15:34

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.