PropertyChanged 事件测试:这是一个好方法吗?

发布于 2024-08-13 15:46:08 字数 975 浏览 2 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

层林尽染 2024-08-20 15:46:08

我使用 小型 Fluent API 来完成此操作。它允许您编写如下测试:

var imageViewModel = TestHelper.GetTestImageViewModel();
imageViewModel.ShouldNotifyOn(s => s.EditModeEnabled)
    When(s => s.Mode = true);

除了简洁之外,我更喜欢这种方法,因为它是类型安全的 - 没有字符串值与您的 API 保持同步。

要测试是否为多个属性引发该事件,您可以编写另一个测试来执行此操作。这将为您提供许多测试,但每个测试都非常小,并且您可以避免断言轮盘

I use a little Fluent API for doing exactly that. It allows you to write tests like this:

var imageViewModel = TestHelper.GetTestImageViewModel();
imageViewModel.ShouldNotifyOn(s => s.EditModeEnabled)
    When(s => s.Mode = true);

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.

尸血腥色 2024-08-20 15:46:08

我相信在您所展示的示例中对 PropertyChanged 事件进行单元测试是个好主意。您可能写错了属性名称字符串,这将导致丢失更新。

使用 WPF 应用程序框架 (WAF) 编写这样的单元测试非常容易:

Person person = new Person();
AssertHelper.PropertyChangedEvent(person, x => x.Name, () => person.Name = "Luke");

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:

Person person = new Person();
AssertHelper.PropertyChangedEvent(person, x => x.Name, () => person.Name = "Luke");
ぃ双果 2024-08-20 15:46:08

与 Mark Seemann 的建议类似,使用 Fluent Assertions,您可以使用简单的好方法来为您包装所有内容,例如:

subject.Should().Raise().PropertyChangeFor(x => x.SomeProperty);

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:

subject.Should().Raise().PropertyChangeFor(x => x.SomeProperty);

Source
https://fluentassertions.com/eventmonitoring/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文