如何使用 XUnit 引发 NotifyCollectionChangedEventArgs
How to raise NotifyCollectionChangedEventArgs with XUnit
我有一个 class 实现 INotifyCollectionChanged
,我想测试是否针对特定场景引发了 CollectionChanged
事件。
我试过下面的代码,但出现编译器错误,到目前为止我找不到解决方案。
[Fact]
public void RaiseOnAddition()
{
Action addition = () => Collection["new key"] = 3;
Assert.Raises<NotifyCollectionChangedEventArgs>(
handler => Collection.CollectionChanged += handler, // compiler error
handler => Collection.CollectionChanged -= handler, // compiler error
addition);
}
Cannot implicitly convert type
'System.EventHandler<System.Collections.Specialized.NotifyCollectionChangedEventArgs>'
to
'System.Collections.Specialized.NotifyCollectionChangedEventHandler'
问题在于 handler
是 EventHandler<NotifyCollectionChangedEventArgs>
我想要 NotifyCollectionChangedEventHandler<NotifyCollectionChangedEventArgs>
.
注意:PropertyChanged
(Assert.PropertyChanged
) 有一个特定的函数要测试,但 CollectionChanged
.
没有
我想除了做这样的事情别无选择,
[Fact]
public void RaiseOnAddition()
{
var timesRaised = 0;
NotifyCollectionChangedEventHandler handler += () => ++timesRaised;
Collection["new key"] = 3;
Assert.Equals(1, timesRaised);
NotifyCollectionChangedEventHandler handler -= () => ++timesRaised;
}
public class TestClass : INotifyCollectionChanged
{
public event NotifyCollectionChangedEventHandler? CollectionChanged;
public void RaiseCollectionChanged()
{
CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}
}
public class TestClassTests
{
[Fact]
public void RaiseOnAddition()
{
var counter = 0;
var testee = new TestClass();
testee.CollectionChanged += (s, e) => counter++;
testee.RaiseCollectionChanged();
testee.RaiseCollectionChanged();
Assert.Equal(2, counter);
}
}
我有一个 class 实现 INotifyCollectionChanged
,我想测试是否针对特定场景引发了 CollectionChanged
事件。
我试过下面的代码,但出现编译器错误,到目前为止我找不到解决方案。
[Fact]
public void RaiseOnAddition()
{
Action addition = () => Collection["new key"] = 3;
Assert.Raises<NotifyCollectionChangedEventArgs>(
handler => Collection.CollectionChanged += handler, // compiler error
handler => Collection.CollectionChanged -= handler, // compiler error
addition);
}
Cannot implicitly convert type 'System.EventHandler<System.Collections.Specialized.NotifyCollectionChangedEventArgs>' to 'System.Collections.Specialized.NotifyCollectionChangedEventHandler'
问题在于 handler
是 EventHandler<NotifyCollectionChangedEventArgs>
我想要 NotifyCollectionChangedEventHandler<NotifyCollectionChangedEventArgs>
.
注意:PropertyChanged
(Assert.PropertyChanged
) 有一个特定的函数要测试,但 CollectionChanged
.
我想除了做这样的事情别无选择,
[Fact]
public void RaiseOnAddition()
{
var timesRaised = 0;
NotifyCollectionChangedEventHandler handler += () => ++timesRaised;
Collection["new key"] = 3;
Assert.Equals(1, timesRaised);
NotifyCollectionChangedEventHandler handler -= () => ++timesRaised;
}
public class TestClass : INotifyCollectionChanged
{
public event NotifyCollectionChangedEventHandler? CollectionChanged;
public void RaiseCollectionChanged()
{
CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}
}
public class TestClassTests
{
[Fact]
public void RaiseOnAddition()
{
var counter = 0;
var testee = new TestClass();
testee.CollectionChanged += (s, e) => counter++;
testee.RaiseCollectionChanged();
testee.RaiseCollectionChanged();
Assert.Equal(2, counter);
}
}