Asier Barrenetxea
Asier Barrenetxea

Reputation: 621

wpf behavior unit test

I am using an attached Behaviours to add drag and drop functionality to my code. So far, everything is working fine, but my problem is when I want to test my behaviour classes.

For example, one of the behaviour classes would be something like the following:

public class DroppableContainerBehavior: Behavior<FrameworkElement>
{

        protected override void OnAttached()
        {
            base.OnAttached();

            AssociatedObject.AllowDrop = true;
            AssociatedObject.Drop += new DragEventHandler(AssociatedObject_Drop);
            AssociatedObject.DragOver += new DragEventHandler(AssociatedObject_DragOver);
            AssociatedObject.DragLeave += new DragEventHandler(AssociatedObject_DragLeave);

        }


        private void AssociatedObject_Drop(object sender, DragEventArgs e)
        {   
    ...
    }         
}

My problem now is when I want to create a unit test for the AssociatedObject_Drop method, i would need to create a DragEventArgs object, but this class is sealed.

I got the impression that I am doing something wrong.. My question is, should i be testing my behaviour classes? Behaviours are related with UI, and usually it's not worth it to test UI. Am i right? Maybe I have to change my behaviours code to make it more testable? any ideas?

Thanks for your help!

Upvotes: 7

Views: 2701

Answers (2)

Avada Kedavra
Avada Kedavra

Reputation: 8691

I would refactor the code and move out any business logic from AssociatedObject_Drop into its own function(s) and then write my unit tests for those functions.

Upvotes: 4

Arseny
Arseny

Reputation: 7361

  1. you can create an object even its class is sealed.

  2. you can test the raise Drop() event in your unit test

  3. you also can test the AssociatedObject_Drop() method logic by extracting its code to other function and write the unit test for this function.

Upvotes: 1

Related Questions