AutoFixture AutoMoq将项添加到ObservableCollection不会引发CollectionChanged事件

时间:2016-01-22 09:20:34

标签: c# unit-testing moq autofixture

我刚刚开始使用AutoFixture,我现在正在享受这些功能。但我刚刚创建了AutoMoqDataAttribute,因为Mark Seemann描述了here

但是现在我试图模拟一个包含ObservableCollection个项目的对象,并且在Sut我订阅CollectionChanged事件并处理新项目时加入。

我的Sut看起来像是:

public class Foo
{
    public Foo(IBar barWithObservableCollection)
    {
        barWithObservableCollection.Items.CollectionChanged += this.OnItemsChanged;
    }

    public ObservableCollection<IFooThing> FooThings
    {
        get;
        private set;
    }

    private void OnItemsChanged(object sender,  NotifyCollectionChangedEventArgs notifyCollectionChangedEventArgs)
    {
        // Handle the new object and build a new IFooThing with it.
        ...
        this.FooThings.Add(fooThing);
    }
}

我的IBar界面很简单,基本上只包含ObservableCollection

public interface IBar
{
    ObservableCollection<IBarThings> Items
    {
        get;
    }
}

所以在我最初的测试实现中,确保处理新项目是使用Moq完全模拟对象并将所有内容捆绑在一起(我将把它留下来,因为它&#39对我的问题不必要)。但正如所说,我试图将其移至使用AutoFixture,现在我的测试看起来像:

[Theory, AutoMoqData]
public void TestNewIBarThingsAreCorrectlyHandled([Frozen]IBar bar, [Frozen]IBarThing barThing, Foo sut)
{
    bar.Items.Add(barThing);

    Assert.Equal(1, sut.FooThings.Count);
}

所以我期待IBar.Items自动设置一个ObservableCollection,它正在订阅,它也是。但是当我调用bar.Items.Add时,不会调用集合已更改处理程序,尽管我可以看到Items对象上的IBar计数增加。

有什么我做错了吗?我是否有错误的结束,我将不得不像以前一样手动设置集合,我不想这样做,因为我喜欢更清晰的语法?

编辑: 在下面的评论之后,我检查了提供给测试和IBar的{​​{1}}对象是否相同,但事实证明这些对象不是同一个对象。我的印象是Sut属性指定每次请求该对象时都会返回相同的对象引用?

0 个答案:

没有答案