简体   繁体   English

如何对WPF用户控件的视图模型进行单元测试

[英]How can I unit test the view models of a WPF user control

How can I mock the creation of ChildViewModel s in ChildrenViewModel : 我如何在ChildrenViewModel模拟创建ChildViewModel

IChildViewModel c = new ChildViewModel(child);
children.Add(c);

I'm using ChildViewModel ( there is no ChildView ! ) since a Child (model) class does not implement INotifyPropertyChanged . 我正在使用ChildViewModel没有ChildView ),因为Child (模型)类未实现INotifyPropertyChanged UPDATE: One of the reasons why I introduced ChildViewModel that encapsulates a Child is a validation requirement. 更新:我引入封装了Child ChildViewModel的原因之一是验证要求。 Domain model objects should always be valid (eg the child's name mustn't consist a digit). 域模型对象应始终 有效 (例如,孩子的名字不得包含数字)。 Nevertheless, the textbox should display invalid values. 但是,文本框应显示无效值。 In this very simple example, ChildrenView consists of a DataGrid that lists the ChildViewModels . 在这个非常简单的示例中, ChildrenView由一个列出了ChildViewModels的DataGrid组成。 The user can see invalid names in the DataGrid "name" column but the child objects are always valid. 用户可以在DataGrid的“名称”列中看到无效的名称,但是子对象始终有效。

ChildrenView is a user control: ChildrenView是一个用户控件:

<views:ChildrenView ChildrenAware="{Binding SelectedItem.ChildrenAware, Mode=OneWay}"/>

ChildrenViewModel is created in the resources of ChildrenView : ChildrenViewModel中的资源创建ChildrenView

<viewModels:ChildrenViewModel x:Key="ViewModel"/>

My aim : I want to test that the ObservableCollection (with type argument ChildViewModel ) is filled up with (mocked) ChildViewModel objects. 我的目标是 :我想测试ObservableCollection(类型参数ChildViewModel )是否已填充(模拟的) ChildViewModel对象。

The problem : The parameterless constructor is executed why I can't use constructor injection (and inject a component that can create ChildViewModel s). 问题 :为什么我不能使用构造函数注入(并注入可以创建ChildViewModel的组件),因此执行了无参数构造函数。

The question : I can see two solutions: Using property injection or a StaticClass that has a set/get property of type IViewModelFactory that I can mock: 问题 :我可以看到两个解决方案:使用属性注入或具有可模拟的IViewModelFactory类型的set / get属性的StaticClass

var mockFactory = new Mock<IViewModelFactory>();
mockFactory.Setup(m => m.CreateChildViewModel(mockChild.Object))
           .Returns(mockChildViewModel);

StaticClass.ViewModelFactory = mockFactory.Object;

Are there any others? 还有其他吗? Which one should I choose? 我应该选择哪一个?

The problem: The parameterless constructor is executed why I can't use constructor injection (and inject a component that can create ChildViewModels). 问题:为什么我不能使用构造函数注入(而注入可以创建ChildViewModels的组件)却执行了无参数构造函数。

Maybe I'm not understanding your question entirely. 也许我不完全理解您的问题。 Why wouldn't you? 你为什么不呢?

If your ViewModel is like 如果您的ViewModel

public class ChildrenViewModel
{
    public ChildrenViewModel()
    {}

    public ChildrenViewModel(IViewModelFactory<IChildViewModel> factory)
    {
        ChildViewModels = new ObservableCollection<IChildViewModel>(factory.Create());
    }

    public ObservableCollection<IChildViewModel> ChildViewModels { get; set; }
}

Then dummy test could be 然后虚拟测试可能是

[TestMethod]
public void ChildViewModelsCreatedTest()
{
    var factory = new Mock<IViewModelFactory<IChildViewModel>>();
    factory.Setup(f => f.Create())
        .Returns(new List<IChildViewModel>() { new ChildViewModel() });

    var vm = new ChildrenViewModel(factory.Object);
    Assert.IsNotNull(vm.ChildViewModels);
    Assert.IsTrue(vm.ChildViewModels.Count == 1);
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何强制wpf中的textBox在单元测试中调用OnLostFocus? - How can I force a textBox in wpf to call OnLostFocus in a unit test? 如何在 wpf 的内容控件中显示视图 - How can I display view inside content control in wpf 如何在wpf用户控件中监听INotifyCollectionChanged - How can I listen to INotifyCollectionChanged in a wpf user control 如何与WPF用户控件中托管的Delphi窗口通信? - How can I communicate with a Delphi window hosted in a WPF User Control? 如何重用WPF用户控件但更改绑定属性? - How can I reuse a WPF user control but change a bound property? 我怎样才能获得用户控件wpf的父级 - how can i get the parent of user control wpf 如何使用XAML和后面的代码序列化WPF用户控件 - How can I serialize wpf user control with xaml and code behind 如何在WPF中对Test View模型和Mvvm应用程序模型进行单元测试 - How to Unit Test View Model and Model of a Mvvm application in wpf 如何将WPF用户控件创建为dll,然后在另一个WPF应用程序中看到它? - How can I create WPF User Control as dll and then see it in another WPF application? 如何使用Windows应用程序驱动程序在UI自动化单元测试期间验证WPF复选框的状态? - How can I verify the state of a WPF checkbox during a UI Automation Unit Test using the Windows Application Driver?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM