0

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?

Stephen Ross
  • 882
  • 6
  • 21
  • Are `bar` and the instance passed to the `Foo` constructor the same object\reference? – Jehof Jan 22 '16 at 09:25
  • Ah that looks like what the issue is, the `IBar` (`bar` object) in the test does have the item added but the one stored within the `Foo` object does not after the `Add`. I believed that the `Frozen` attribute specified that you would have the same instance of the object on each request to the `Fixture`? – Stephen Ross Jan 22 '16 at 09:37
  • 1
    Based on the information given here, I can't get the code to compile. Please provide [a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – Mark Seemann Jan 23 '16 at 20:50

0 Answers0