我刚刚开始使用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
属性指定每次请求该对象时都会返回相同的对象引用?