wpf behavior unit test
Asked Answered
D

2

7

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!

Denotation answered 5/7, 2011 at 9:12 Comment(0)
S
4

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.

Stay answered 5/7, 2011 at 9:16 Comment(0)
G
1
  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.
Guenzi answered 5/7, 2011 at 9:25 Comment(4)
and do you know how I could create an object even its class is sealed?Denotation
A sealed class cannot be inherited but you can create an instance of this class. see msdn.microsoft.com/en-us/library/88c54tsw%28v=vs.71%29.aspxGuenzi
um, sorry the problem when doing new DragEventArgs() is not that is sealed. I can't instantiate it because the constructor is internal.Denotation
@Asier Actually I don't see why you need to create this Argument all you need in to sign a handler to Drop event.in case you still want to create this you may create a Mock for this type DragEventArgs.Guenzi

© 2022 - 2024 — McMap. All rights reserved.