vendredi 18 septembre 2015

How do I NUnit test a RelayCommand that contains an Async Service Call?

I have a RelayCommand that I am trying to test. The RelayCommand contains a Service Call to Authenticate my user. Shown below:

private MvxCommand _signIn;
public MvxCommand SignIn
{
    get
    {
        return _signIn ?? (_signIn = new MvxCommand(() =>
        {
            BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.Transport);
            EndpointAddress endpoint = new EndpointAddress(AppResources.AuthService);
            var client = new MyService(binding, endpoint);
            client.AuthorizeCompleted += ((sender, args) =>
            {
                try
                {
                    if (args.Result)
                    {
                        //Success.. Carry on
                    }
                }
                catch (Exception ex)
                {
                    //AccessDenied Exception thrown by service
                    if (ex.InnerException != null && string.Equals(ex.InnerException.Message, "Access is denied.", StringComparison.CurrentCultureIgnoreCase))
                    {
                        //Display Message.. Incorrect Credentials
                    }
                    else
                    {
                        //Other error... Service down?
                    }
                }
            });

            client.AuthorizeAsync(UserName, Password, null);

        }));
    }
}

But now I am using NUnit to test my ViewModels and I am stumped on how to test my RelayCommand

I can do:

[Test]
public void PerformTest()
{
    ViewModel.SignIn.Execute();
}

But this returns no information on whether the SignIn method succeeded or not.

How do I test a RelayCommand containing a Service Call?

Aucun commentaire:

Enregistrer un commentaire