コード例 #1
0
        public void ShouldNotifyObserversOfGameResult()
        {
            var subject   = new SportsAggregator();
            var observer1 = new NewsPaper();
            var observer2 = new RadioStation();

            subject.RegisterObserver(observer1);
            subject.RegisterObserver(observer2);
            var gameResult = new GameResult
            {
                GameDate     = DateTime.Now,
                LosingScore  = 1,
                LosingTeam   = "Chicago Cubs",
                WinningScore = 5,
                WinningTeam  = "Cincinnati Reds"
            };

            subject.AddGameResult(gameResult);
            Assert.Equal(1, observer1.Results.Count);
            Assert.Equal(1, observer2.Results.Count);
            Assert.Same(gameResult, observer1.Results[0]);
            Assert.Same(gameResult, observer2.Results[0]);
            subject.UnregisterObserver(observer1);
            subject.UnregisterObserver(observer2);
            Assert.Equal(0, subject.Observers.Count);
        }
コード例 #2
0
        public void SportsAggregator_AddGameResult()
        {
            // Arrange
            var subject   = new SportsAggregator();
            var observer1 = new NewsPaper();
            var observer2 = new RadioStation();

            subject.RegisterObserver(observer1);
            subject.RegisterObserver(observer2);
            var gameResult = new GameResult
            {
                GameDate     = DateTime.Now,
                LosingScore  = 1,
                LosingTeam   = "Mariners",
                WinningScore = 10,
                WinningTeam  = "Astros"
            };

            // Action
            subject.AddGameResult(gameResult);

            // Assert
            Assert.AreEqual(1, observer1.Results.Count);
            Assert.AreEqual(1, observer2.Results.Count);
            Assert.AreSame(gameResult, observer1.Results[0]);
            Assert.AreSame(gameResult, observer2.Results[0]);

            // Action
            subject.UnregisterObserver(observer1);
            subject.UnregisterObserver(observer2);

            Assert.IsFalse(subject.Observers.Any());
        }
コード例 #3
0
        public void DoesntHandleNullObjectsCorrectly()
        {
            var subject   = new SportsAggregator();
            var observer1 = new NewsPaper();
            var observer2 = new RadioStation();

            subject.RegisterObserver(observer1);
            subject.RegisterObserver(observer2);
            observer1 = null;
            GC.Collect();
            GC.WaitForPendingFinalizers();
            var gameResult = new GameResult
            {
                GameDate     = DateTime.Now,
                LosingScore  = 1,
                LosingTeam   = "Chicago Cubs",
                WinningScore = 5,
                WinningTeam  = "Cincinnati Reds"
            };

            subject.AddGameResult(gameResult);
            subject.UnregisterObserver(observer2);
            //subject.UnregisterObserver(observer1);
            Assert.Equal(1, subject.Observers.Count);
        }