PropertyChanged 事件测试:这是一个好方法吗?
我正在使用 MVVM 模式开发 WPF 应用程序。我的 ViewModel 的代码如下:
public bool EditModeEnabled
{
get { return _EditModeEnabled; }
set
{
_ModeEditModeEnabled = value;
OnPropertyChanged("EditModeEnabled");
OnPropertyChanged("CommentTextBoxVisibility");
}
}
OnPropertyChanged
是基类的虚拟方法,它只会引发 PropertyChanged
事件。 我想测试 PropertyChanged
事件引发,并且有我的测试方法:
public void EditModeEnabledTest()
{
var imageViewModel = TestHelper.GetTestImageViewModel();
var firedEvents = new List<string>();
imageViewModel.PropertyChanged += ((sender, e) => firedEvents.Add(e.PropertyName));
imageViewModel.Mode = true;
Assert.AreEqual(firedEvents.Count, 2);
Assert.IsTrue(firedEvents.Contains("EditModeEnabled"));
Assert.IsTrue(firedEvents.Contains("CommentTextBoxVisibility"));
...
}
Is it a good way to test ProprtyChanged event?
I'm developing WPF applications using MVVM pattern. I have ViewModel with code like this:
public bool EditModeEnabled
{
get { return _EditModeEnabled; }
set
{
_ModeEditModeEnabled = value;
OnPropertyChanged("EditModeEnabled");
OnPropertyChanged("CommentTextBoxVisibility");
}
}
OnPropertyChanged
is virtual method of base class which just raise PropertyChanged
event.
I want to test PropertyChanged
event raising and there my test method:
public void EditModeEnabledTest()
{
var imageViewModel = TestHelper.GetTestImageViewModel();
var firedEvents = new List<string>();
imageViewModel.PropertyChanged += ((sender, e) => firedEvents.Add(e.PropertyName));
imageViewModel.Mode = true;
Assert.AreEqual(firedEvents.Count, 2);
Assert.IsTrue(firedEvents.Contains("EditModeEnabled"));
Assert.IsTrue(firedEvents.Contains("CommentTextBoxVisibility"));
...
}
Is it a good way to test ProprtyChanged event?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我使用 小型 Fluent API 来完成此操作。它允许您编写如下测试:
除了简洁之外,我更喜欢这种方法,因为它是类型安全的 - 没有字符串值与您的 API 保持同步。
要测试是否为多个属性引发该事件,您可以编写另一个测试来执行此操作。这将为您提供许多测试,但每个测试都非常小,并且您可以避免断言轮盘。
I use a little Fluent API for doing exactly that. It allows you to write tests like this:
Besides being succinct, I prefer this approach because it's type-safe - no string values to keep in sync with your API.
To test that the event is being raised for more than one property, you can just write another test that does this. This will give you many tests, but each will be very small and you avoid Assertion Roulette.
我相信在您所展示的示例中对 PropertyChanged 事件进行单元测试是个好主意。您可能写错了属性名称字符串,这将导致丢失更新。
使用 WPF 应用程序框架 (WAF) 编写这样的单元测试非常容易:
I believe it’s a good idea to unit test the PropertyChanged event in the example you have shown. You might have written the property name string wrong which would result in a missing update.
With the WPF Application Framework (WAF) it’s very easy to write such a unit test:
与 Mark Seemann 的建议类似,使用 Fluent Assertions,您可以使用简单的好方法来为您包装所有内容,例如:
Source
https:// Fluentassertions.com/eventmonitoring/
Similar to the suggestion of Mark Seemann, with Fluent Assertions you have simple nice methods that wrap it all up for you, like:
Source
https://fluentassertions.com/eventmonitoring/