Wpf unit test behavior

I use attached Behaviors 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 behavior classes.

For example, one of the behavior classes will look something like this:

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) { ... } } 

Now the problem is when I want to create a unit test for the AssociatedObject_Drop method, I will need to create a DragEventArgs object, but this class is sealed.

I got the impression that I'm doing something wrong. My question is: should I test my behavior classes? The behavior is related to the user interface, and usually it is not worth testing the interface. I'm right? Maybe I need to change the behavior code to make it more verifiable? any ideas?

Thanks for your help!

+6
source share
2 answers

I would reorganize the code and move any business logic from AssociatedObject_Drop to my own function (s), and then write my unit tests for these functions.

+4
source
  • you can create an object, even its class is sealed.

  • you can test raise drop () event in unit test

  • you can also test the logic of the AssociatedObject_Drop () method by extracting your code into another function and write unit test for this function.
+1
source

Source: https://habr.com/ru/post/892009/


All Articles