gpt4 book ai didi

c# - 单元测试 NotifyPropertyChanged()

转载 作者:行者123 更新时间:2023-11-30 21:00:49 26 4
gpt4 key购买 nike

测试此属性的最佳方法是什么:

  public string ThreadId {
get { return _threadId; }
set {
_threadId = value;
NotifyPropertyChanged();
}
}

到目前为止我有这个测试:

    [Fact]
public void ThreadIdTest() {
compassLogData.ThreadId = "[11]";
const string expected = "[11]";
string actual = compassLogData.ThreadId;
Assert.Equal(expected, actual);
}

但我需要一个测试 NotifyPropertyChanged() 用于更新用户界面。

最佳答案

一个简单的方法是这样做:

var notified = false;
compassLogData.PropertyChanged += (s, e) =>
{
if(e.PropertyName == "ThreadId")
notified = true;
};

compassLogData.ThreadId = "[11]";
Assert.True(notified);

关于c# - 单元测试 NotifyPropertyChanged(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14705953/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com