WPF中数据验证的单元测试 [英] Unit test on data validation in WPF

查看:167
本文介绍了WPF中数据验证的单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参阅示例。

我说我有数据模型

Please see the example.
Say I have the data model as

public class Model
{
    [Required(ErrorMessage = "You must enter a username.")]
    [StringLength(10, MinimumLength = 4,
        ErrorMessage = "The username must be between 4 and 10 characters long")]
    [RegularExpression(@"^[a-zA-Z]+$", ErrorMessage = "The username must only contain letters (a-z, A-Z).")]
    public string Username { get; set; }
 
    [Required(ErrorMessage = "You must enter a name.")]
    public string Name { get; set; }
}



现在我想对验证进行单元测试。例如,如果我输入无效的章程,然后单击保存按钮,屏幕将显示警告消息。消息来自验证。我的问题是我应该直接在ViewModel或Model上进行吗?



如何模拟ICommand?



我尝试过:



我可能会在ViewModel单元测试中做。假设模拟ViewModel然后模拟模型的属性。

推荐答案

,ErrorMessage =用户名必须只包含字母(az,AZ)。)]
公共字符串用户名{get; set;}

[必需(ErrorMessage =你必须输入一个名字。)]
公共字符串名称{get; set;}
}
", ErrorMessage = "The username must only contain letters (a-z, A-Z).")] public string Username { get; set; } [Required(ErrorMessage = "You must enter a name.")] public string Name { get; set; } }



现在我想对验证进行单元测试。例如,如果我输入无效包机然后单击一个保存按钮,屏幕将显示一条警告消息。消息来自验证。我的问题是我应该直接在ViewModel或Model上进行吗?



我该如何嘲笑ICommand?



我尝试过的事情:



我可以在ViewModel单元测试中做。假设模拟ViewModel然后模拟模型的属性。


我用谷歌搜索单元测试mvc模型验证,这是第一个结果



TDD ASP.NET MVC第4部分:单元测试视图模型验证 - Visual Studio Magazine [ ^ ]



如果你自己google你会找到别的。一般来说,虽然你应该只对自己的代码进行单元测试,但模型验证是微软的代码,你应该假设他们自己测试了它并且它是有效的。但是,如果您正在进行正则表达式验证,那么在测试中您的正则表达式按预期工作可能仍然有价值,因此在这种情况下进行单元测试可能没问题。
I googled "unit test mvc model validation" and this was the first result

TDD for ASP.NET MVC Part 4: Unit Testing View Model Validation -- Visual Studio Magazine[^]

If you google yourself you'll find others. In general though you should only unit test your own code, the model validation is Microsoft's code and you should simply assume they have tested it themselves and that it works. Saying that, however, as you have regex validation there probably is still value in testing that your regex is working as expected so unit testing in this case is probably ok to do.


这篇关于WPF中数据验证的单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆