public void AssignToRoute_withAnItinerary_dontCallUnsubscribedHandlersOf_ItineraryChanged() { // arrange: GList mocks = new GList(); TrackingId identifier = new TrackingId("CARGO01"); DateTime arrivalDate = DateTime.Now + TimeSpan.FromDays(30); IItinerary itinerary = MockRepository.GenerateStrictMock<IItinerary>(); itinerary.Expect(i => i.Equals(null)).Return(false).Repeat.Any(); itinerary.Expect(i => i.FinalArrivalDate).Return(arrivalDate).Repeat.Any(); mocks.Add(itinerary); IRouteSpecification route = MockRepository.GenerateStrictMock<IRouteSpecification>(); route.Expect(r => r.IsSatisfiedBy(itinerary)).Return(true).Repeat.AtLeastOnce(); mocks.Add(route); CargoState mockState1 = MockRepository.GeneratePartialMock<CargoState>(identifier, route); CargoState mockState2 = MockRepository.GeneratePartialMock<CargoState>(mockState1, itinerary); mockState1.Expect(s => s.AssignToRoute(itinerary)).Return(mockState2).Repeat.Once(); mockState1.Expect(s => s.Equals(mockState2)).Return(false).Repeat.Any(); mocks.Add(mockState1); mocks.Add(mockState2); ChangeEventArgs<IItinerary> eventArguments = null; ICargo eventSender = null; // act: EventHandler<ChangeEventArgs<IItinerary>> handler = delegate(object sender, ChangeEventArgs<IItinerary> e) { eventArguments = e; eventSender = sender as ICargo; }; TCargo underTest = new FakeCargo(mockState1); underTest.ItineraryChanged += handler; underTest.ItineraryChanged -= handler; underTest.AssignToRoute(itinerary); // assert: Assert.AreSame(itinerary, underTest.Itinerary); Assert.IsNull(eventArguments); Assert.IsNull(eventSender); foreach(object mock in mocks) mock.VerifyAllExpectations(); }
public void AssignToRoute_withAnItinerary_dontBlockExceptionsFromCurrentState() { // arrange: GList mocks = new GList(); TrackingId identifier = new TrackingId("CARGO01"); Exception eThrown = new Exception("Catch me."); DateTime arrivalDate = DateTime.Now + TimeSpan.FromDays(30); IItinerary itinerary = MockRepository.GenerateStrictMock<IItinerary>(); itinerary.Expect(i => i.Equals(null)).Return(false).Repeat.Any(); itinerary.Expect(i => i.FinalArrivalDate).Return(arrivalDate).Repeat.Any(); mocks.Add(itinerary); IRouteSpecification route = MockRepository.GenerateStrictMock<IRouteSpecification>(); mocks.Add(route); CargoState mockState1 = MockRepository.GeneratePartialMock<CargoState>(identifier, route); mockState1.Expect(s => s.AssignToRoute(itinerary)).Throw(eThrown).Repeat.Any(); mocks.Add(mockState1); ChangeEventArgs<IItinerary> eventArguments = null; ICargo eventSender = null; // act: TCargo underTest = new FakeCargo(mockState1); underTest.ItineraryChanged += delegate(object sender, ChangeEventArgs<IItinerary> e) { eventArguments = e; eventSender = sender as ICargo; }; Assert.Throws<Exception>(delegate{ underTest.AssignToRoute(itinerary);},"Catch me."); // assert: Assert.IsNull(underTest.Itinerary); Assert.IsNull(eventArguments); Assert.IsNull(eventSender); foreach(object mock in mocks) mock.VerifyAllExpectations(); }