Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
add comment (requires an account with 50 reputation)

1 Answer

up vote 1 down vote accepted

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.

share|improve this answer
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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