I'm just starting out using AutoFixture
and I'm enjoying the features just now. But I've just created an AutoMoqDataAttribute
as Mark Seemann describes here.
But now I'm trying to mock an object that contains an ObservableCollection
of items and in my Sut
I am subscribing to the CollectionChanged
event and handling when new items are added.
My Sut
looks like:
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);
}
}
My IBar
interface is simple and basically only contains the ObservableCollection
:
public interface IBar
{
ObservableCollection<IBarThings> Items
{
get;
}
}
So in my original implementation of the test to make sure that new items were handled was with full mocking of the objects using Moq
and tying everything together myself (I'll leave this out as it's unnecessary for my question). But as said I've attempted to move this to using AutoFixture
and now my tests looks like:
[Theory, AutoMoqData]
public void TestNewIBarThingsAreCorrectlyHandled([Frozen]IBar bar, [Frozen]IBarThing barThing, Foo sut)
{
bar.Items.Add(barThing);
Assert.Equal(1, sut.FooThings.Count);
}
So I was expecting that the IBar.Items
was auto setting up an ObservableCollection
which it is and this was being subscribed to, which it also is. But when I do the call to bar.Items.Add
the collection changed handler is not not called, although I can see that the Items
count is incremented on the IBar
object.
Is there something that I'm doing wrong? Have I got the wrong end of the stick and I will have to manually setup the collection as before, I'd rather not have to do this as I'm enjoying the cleaner syntax?
EDIT:
After the comment below I checked that the IBar
object provided to the tests and to the Sut
were the same, but it turns out that these are not the same object. I was under the impression that the [Frozen]
attribute specified that each time that object was requested the same object reference would be returned?