简体   繁体   中英

Unit testing of WPF application

Here is a part of a code of a simple WPF application: 3 textboxes, a dropdownList and a button. By clicking a button, there will be checking of input values.

   private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        if (textBox1.Text.Length>127)
            throw new ArgumentException();
        if (string.IsNullOrEmpty(textBox2.Text))
            errorsList.Add("You must to fill out textbox2");
        else if (string.IsNullOrEmpty(textBox3.Text))
           errorsList.Add("You must to fill out textbox3"); 
       else if
        {
            Regex regex = new Regex(@".........");
            Match match = regex.Match(emailTxt.Text);
            if (!match.Success)
                errorsList.Add("e-mail is inlvalid");
        }
        //.....
     }

I have to test it by using any Unit testing Framework. I wonder is it possible to do Unit testing here? I guess it is not, right?

It is not possible to unit test the current code you have without refactoring. You should encapsulate that logic in a ViewModel class. I guess you can have something like

DoTheJob(string1,string2,string3,...)

and error/errorList/exList as ObservableCollections into the viev model too. With these precondition you can write a suite of unit tests checking your code behavior.

So basically you need a viewmodel class which represents your UI

        public class ViewModel
    {
        public ViewModel()
        {
            ButtonClickCommand = new RelayCommand(Validate);
        }

        private void Validate()
        {
            if (Text1.Length > 127) 
                throw new ArgumentException();
            if (string.IsNullOrEmpty(Text2)) 
                ErrorList.Add("You must to fill out textbox2");
            else if (string.IsNullOrEmpty(Text3)) 
                ErrorList.Add("You must to fill out textbox3");
            else
            {
                Regex regex = new Regex(@".........");
                Match match = regex.Match(Email);
                if (!match.Success) ErrorList.Add("e-mail is inlvalid");
            }
        }

        public string Text1 { get; set; }
        public string Text2 { get; set; }
        public string Text3 { get; set; }
        public string Email { get; set; }
        public ObservableCollection<string> ErrorList { get; set; }
        public ICommand ButtonClickCommand { get; private set; }
    }

And instance of ViewModel should be attached to DataContext property of your control/window.

In this approach, you can unit test your ViewModel all the way you want :)

Well if you command bind your button

<Button Command="{Binding SayHello}">Hello</Button>

Then in your unit test you should be able to execute the command on the button.

var button = GetMyButton();
button.Command.Execute(new object());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM