WPF行为单元测试
我正在使用附加的行为向我的代码添加拖放功能。 到目前为止,一切工作正常,但我的问题是当我想测试我的行为类时。
例如,行为类之一如下所示:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会重构代码,并将所有业务逻辑从
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.即使它的类是密封的,您也可以创建一个对象。
您可以在单元测试中测试 raise Drop() 事件
you can create an object even its class is sealed.
you can test the raise Drop() event in your unit test