WPF行为单元测试

发布于 2024-11-18 15:14:18 字数 911 浏览 2 评论 0原文

我正在使用附加的行为向我的代码添加拖放功能。 到目前为止,一切工作正常,但我的问题是当我想测试我的行为类时。

例如,行为类之一如下所示:

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

我现在的问题是,当我想为 AssociatedObject_Drop 方法创建单元测试时,我需要创建一个 DragEventArgs 对象,但此类是密封的。

我感觉我做错了什么.. 我的问题是,我应该测试我的行为课程吗?行为与 UI 相关,通常不值得测试 UI。我说得对吗? 也许我必须更改我的行为代码以使其更易于测试?有什么想法吗?

感谢您的帮助!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

叹倦 2024-11-25 15:14:18

我会重构代码,并将所有业务逻辑从 AssociatedObject_Drop 移至其自己的函数中,然后为这些函数编写单元测试。

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.

星軌x 2024-11-25 15:14:18
  1. 即使它的类是密封的,您也可以创建一个对象。

  2. 您可以在单元测试中测试 raise Drop() 事件

  3. 您还可以通过将其代码提取到其他函数来测试 AssociatedObject_Drop() 方法逻辑并为此函数编写单元测试。
  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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文