如何测试是否显示警报/错误消息

时间:2017-11-15 07:37:34

标签: c# unit-testing uwp

我的VM上有一个属性,有验证:

private string _test;
[Required(ErrorMessage = "Required.")]
public string Test {
    get { return _test; }
    set { SetProperty(ref _test, value); }
}

对ValidationAttribute的引用:https://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.requiredattribute(v=vs.110).aspx

这是我的观点中的控件:

<TextBox Grid.Column="1" 
        Text="{Binding Test, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
        Style="{StaticResource TextBoxStyle}"/>
<TextBlock Grid.Column="1" Grid.Row="1" Text="{Binding Errors[Test][0]}"
        Style="{StaticResource ViewErrorStyle}"/>

如果显示错误消息,我如何进行单元测试?

由于

1 个答案:

答案 0 :(得分:0)

您不能直接在uwp上使用MVC注释。您可以使用棱镜验证。有关更多信息,请参阅以下链接。 https://blogs.u2u.be/diederik/post/User-input-validation-with-Prism-and-data-annotations-on-the-UWP

视图中的一些示例代码:

    <TextBox x:Name="Email" 
             Header="Email"
             Text="{x:Bind Path=ViewModel.Email, Mode=TwoWay}"
             MaxLength="50"
             InputScope="EmailSmtpAddress" 
             AutomationProperties.IsRequiredForForm="True" />

    <TextBlock Text="{x:Bind Path=ViewModel.Error_Email, Mode=TwoWay}"
               Foreground="Red"
               HorizontalAlignment="Right" />

视图模型中的一些示例代码:

    [EmailAddress(ErrorMessage = "Please enter valid email address")]
    [Required]
    public string Email
    {
        get { return _Email ?? string.Empty; }
        set
        {
            SetProperty(ref _Email, value);
            Error_Email = Errors[nameof(Email)].Count > 0 ? Errors[nameof(Email)][0] : string.Empty;
        }
    }

    public string Error_Email
    {
        get { return _error_Email ?? string.Empty; }
        set { SetProperty(ref _error_Email, value); }
    }