using System; using System.Collections.Generic; #if UNITTEST using NUnit.Framework; using NMock2; namespace Nuclex.Support.Collections { /// Unit Test for the observable collection class [TestFixture] public class ObservableCollectionTest { /// Interface used to test the observable collection. public interface IObservableCollectionSubscriber { /// Called when the collection is about to clear its contents /// Collection that is clearing its contents /// Not used void Clearing(object sender, EventArgs e); /// Called when an item is added to the collection /// Collection to which an item is being added /// Contains the item that is being added void ItemAdded(object sender, ObservableCollection.ItemEventArgs e); /// Called when an item is removed from the collection /// Collection from which an item is being removed /// Contains the item that is being removed void ItemRemoved(object sender, ObservableCollection.ItemEventArgs e); } /// Initialization routine executed before each test is run [SetUp] public void Setup() { this.mockery = new Mockery(); this.mockedSubscriber = this.mockery.NewMock(); this.observedCollection = new ObservableCollection(); this.observedCollection.Clearing += new EventHandler(this.mockedSubscriber.Clearing); this.observedCollection.ItemAdded += new EventHandler.ItemEventArgs>( this.mockedSubscriber.ItemAdded ); this.observedCollection.ItemRemoved += new EventHandler.ItemEventArgs>( this.mockedSubscriber.ItemRemoved ); } /// Tests whether the Clearing event is fired [Test] public void TestClearingEvent() { Expect.Once.On(this.mockedSubscriber). Method("Clearing"); this.observedCollection.Clear(); } /// Tests whether the ItemAdded event is fired [Test] public void TestItemAddedEvent() { Expect.Once.On(this.mockedSubscriber). Method("ItemAdded"). WithAnyArguments(); this.observedCollection.Add(123); } /// Tests whether the ItemRemoved event is fired [Test] public void TestItemRemovedEvent() { Expect.Once.On(this.mockedSubscriber). Method("ItemAdded"). WithAnyArguments(); Expect.Once.On(this.mockedSubscriber). Method("ItemRemoved"). WithAnyArguments(); this.observedCollection.Add(123); this.observedCollection.Remove(123); } /// Mock object factory private Mockery mockery; /// The mocked observable collection subscriber private IObservableCollectionSubscriber mockedSubscriber; /// An observable collection to which a mock will be subscribed private ObservableCollection observedCollection; } } // namespace Nuclex.Support.Collections #endif // UNITTEST