[英]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.