1

I m doing unit test for one of my app in WP7. I want to check whether the button_click function works properly. When I try to call the button_click function from my unit testcode like below

        CheckUrVacabolary.MainPage cpage = new CheckUrVacabolary.MainPage();
        cpage.txtFind.Text = "home";
        cpage.butMeaning_Click(cpage,null);

But the eventhandler(OnDefineInDictCompleted) inside the button_click(OnDefineInDictCompleted)
is not getting called. Here is the code

    internal void butMeaning_Click(object sender, RoutedEventArgs e)
    {
        graphPass.Visibility = Visibility.Collapsed;
        graphFail.Visibility = Visibility.Collapsed;

        if (txtFind.Text.ToString() != "Enter the word")
        {
            butNext.IsEnabled = true;
            DictServiceSoapClient client = GetDictServiceSoapClient();
            String meaningfor;
            if (txtRandomWord.Text.Trim().Length != 0)
            {
                txtRandomWord.Text = "";
                meaningfor = wordToGuess;
            }
            else
            {
                meaningfor = txtFind.Text.Trim().ToString();
            }

            if (meaningfor.Length != 0)
            {
                client.DefineInDictCompleted +=
                    new EventHandler<DefineInDictCompletedEventArgs
                            (OnDefineInDictCompleted);
                client.DefineInDictAsync("gcide", meaningfor);
            }
        }
    }

I m not using MVVM model in my app. Is there any way I could call the event handler also.

Enigmativity
  • 113,464
  • 11
  • 89
  • 172

1 Answers1

1

The easy answer would be: do use MVVM nonetheless. One of its benefits is easier unit testing. But if you choose not to, you should make a separate method in a service-like class that is called by the event handler but can also be called from a unit test.

The method should contain only the business logic you want to test and not the UI behaviour. (With a view model you could even test UI behaviour, because it is abstracted). That means it should have arguments for the values you obtain from controls like txtRandomWord.

The event handler that client connects to is a problem.

First, the life cycle of each client instance you create by pressing the button is extended to that of the page, which introduces a potential memory leak.

Second, I assume OnDefineInDictCompleted is a method in your page, so you should extract that from the page as well in a way that it is accessible to unit tests. If that method touches UI elements, this may be a real headache. Again, a strong case for a view model.

Community
  • 1
  • 1
Gert Arnold
  • 105,341
  • 31
  • 202
  • 291