public static void Run() { Console.WriteLine($"{Environment.NewLine}*** OBSERVER PATTERN ***{Environment.NewLine}"); // Make a new bike manufacturer; IBikeManufacturer manufacturer = new BikeManufacturer(); // Make some shops IBikeShop roadBikeShop = new RoadBikeShop(); IBikeShop mountainBikeShop = new MountainBikeShop(); // Register the shops for updates WeakReference <IBikeShop> roadBikeShopRef = manufacturer.RegisterShop(roadBikeShop); WeakReference <IBikeShop> mountainBikehopRef = manufacturer.RegisterShop(mountainBikeShop); // Announce some new bikes manufacturer.NotifyShops(new BikeModel { Name = "Mountain King", Type = BikeType.Mountain }); manufacturer.NotifyShops(new BikeModel { Name = "Road Racer", Type = BikeType.Road }); }
public void BikeManufacturer_NotifyShops_HandlesNullReferences() { // Arrange IBikeManufacturer manufacturer = new BikeManufacturer(); IBikeShop mShop = new MountainBikeShop(); WeakReference <IBikeShop> mShopRef = manufacturer.RegisterShop(mShop); // null out the shop mShop = null; GC.Collect(); GC.WaitForPendingFinalizers(); // Act manufacturer.NotifyShops(new BikeModel { Name = "test", Type = Common.BikeType.Mountain }); // Assert // @Markmc27 this doesn't work if I mock it because I've first got to make the // unregister call virtual and then the null mShop reference doesn't seemto be null any more. // I think mock is caching it or copying it somewhere and preventing the GC from doing it's thing. //mockManufacturer.Verify(m => m.UnregisterShop(It.IsAny<WeakReference<IBikeShop>>()), Times.Once); }
public void BikeManufacturer_NotifyShops_NotifiesObservers() { // Arrange IBikeManufacturer manufacturer = new BikeManufacturer(); Mock <IBikeShop> mockShop = new Mock <IBikeShop>(); WeakReference <IBikeShop> mShopRef = manufacturer.RegisterShop(mockShop.Object); // Act manufacturer.NotifyShops(new BikeModel { Name = "test", Type = Common.BikeType.Mountain }); // Assert mockShop.Verify(m => m.NotifyAboutNewBike(It.IsAny <BikeModel>()), Times.Once); }