public void DepartFrom_aLocation_dontBlockInvalidOperationExceptionFromCurrentState() { // arrange: VoyageNumber number = new VoyageNumber("VYG01"); ISchedule schedule = MockRepository.GenerateStrictMock <ISchedule>(); ILocation location = MockRepository.GenerateStrictMock <ILocation>(); VoyageState state = MockRepository.GeneratePartialMock <VoyageState>(number, schedule); state.Expect(s => s.DepartFrom(location)).Throw(new InvalidOperationException()).Repeat.Once(); VoyageEventArgs eventArguments = null; IVoyage eventSender = null; // act: IVoyage voyage = MockRepository.GeneratePartialMock <Challenge00.DDDSample.Voyage.Voyage>(state); voyage.Departed += delegate(object sender, VoyageEventArgs e) { eventArguments = e; eventSender = sender as IVoyage; }; Assert.Throws <InvalidOperationException>(delegate { voyage.DepartFrom(location); }); // assert: Assert.IsNull(eventSender); Assert.IsNull(eventArguments); state.VerifyAllExpectations(); schedule.VerifyAllExpectations(); location.VerifyAllExpectations(); }
public void DepartFrom_previousLocation_dontFireDeparted() { // arrange: VoyageNumber number = new VoyageNumber("VYG01"); ISchedule schedule = MockRepository.GenerateStrictMock <ISchedule>(); ILocation location = MockRepository.GenerateStrictMock <ILocation>(); VoyageState state = MockRepository.GeneratePartialMock <VoyageState>(number, schedule); state.Expect(s => s.DepartFrom(location)).Return(state).Repeat.Once(); state.Expect(s => s.Equals(state)).Return(true).Repeat.Once(); VoyageEventArgs eventArguments = null; IVoyage eventSender = null; // act: IVoyage voyage = MockRepository.GeneratePartialMock <Challenge00.DDDSample.Voyage.Voyage>(state); voyage.Departed += delegate(object sender, VoyageEventArgs e) { eventArguments = e; eventSender = sender as IVoyage; }; voyage.DepartFrom(location); // assert: Assert.AreEqual(number, voyage.Number); Assert.IsNull(eventSender); Assert.IsNull(eventArguments); state.VerifyAllExpectations(); schedule.VerifyAllExpectations(); location.VerifyAllExpectations(); }
public void DepartFrom_aLocation_dontCallUnsubscribedHandlersOf_Departed() { // arrange: VoyageNumber number = new VoyageNumber("VYG01"); UnLocode departure = new UnLocode("DPLOC"); UnLocode arrival = new UnLocode("ARLOC"); ISchedule schedule = MockRepository.GenerateStrictMock <ISchedule>(); ILocation location = MockRepository.GenerateStrictMock <ILocation>(); VoyageState state = MockRepository.GeneratePartialMock <VoyageState>(number, schedule); VoyageState state2 = MockRepository.GeneratePartialMock <VoyageState>(number, schedule); state.Expect(s => s.DepartFrom(location)).Return(state2).Repeat.Once(); state.Expect(s => s.Equals(state2)).Return(false).Repeat.Once(); state2.Expect(s => s.LastKnownLocation).Return(departure).Repeat.Once(); state2.Expect(s => s.NextExpectedLocation).Return(arrival).Repeat.Once(); VoyageEventArgs eventArguments = null; IVoyage eventSender = null; // act: IVoyage voyage = MockRepository.GeneratePartialMock <Challenge00.DDDSample.Voyage.Voyage>(state); EventHandler <VoyageEventArgs> handler = delegate(object sender, VoyageEventArgs e) { eventArguments = e; eventSender = sender as IVoyage; }; voyage.Departed += handler; voyage.Departed -= handler; voyage.DepartFrom(location); // assert: Assert.AreEqual(number, voyage.Number); Assert.IsNull(eventSender); Assert.IsNull(eventArguments); state.VerifyAllExpectations(); schedule.VerifyAllExpectations(); location.VerifyAllExpectations(); state2.VerifyAllExpectations(); }
public void DepartFrom_currentLocation_fireDeparted() { // arrange: VoyageNumber number = new VoyageNumber("VYG01"); UnLocode departure = new UnLocode("DPLOC"); UnLocode arrival = new UnLocode("ARLOC"); ISchedule schedule = MockRepository.GenerateStrictMock <ISchedule>(); ILocation location = MockRepository.GenerateStrictMock <ILocation>(); VoyageState state = MockRepository.GeneratePartialMock <VoyageState>(number, schedule); VoyageState state2 = MockRepository.GeneratePartialMock <VoyageState>(number, schedule); state.Expect(s => s.DepartFrom(location)).Return(state2).Repeat.Once(); state.Expect(s => s.Equals(state2)).Return(false).Repeat.Once(); state2.Expect(s => s.LastKnownLocation).Return(departure).Repeat.Any(); state2.Expect(s => s.NextExpectedLocation).Return(arrival).Repeat.Any(); VoyageEventArgs eventArguments = null; IVoyage eventSender = null; // act: IVoyage voyage = MockRepository.GeneratePartialMock <Challenge00.DDDSample.Voyage.Voyage>(state); voyage.Departed += delegate(object sender, VoyageEventArgs e) { eventArguments = e; eventSender = sender as IVoyage; }; voyage.DepartFrom(location); // assert: Assert.AreEqual(number, voyage.Number); Assert.AreSame(voyage, eventSender); Assert.AreSame(departure, eventArguments.PreviousLocation); Assert.AreSame(arrival, eventArguments.DestinationLocation); state.VerifyAllExpectations(); schedule.VerifyAllExpectations(); location.VerifyAllExpectations(); state2.VerifyAllExpectations(); }
public void DepartFrom_nullLocation_throwsArgumentNullException() { // arrange: VoyageNumber number = new VoyageNumber("VYG01"); ISchedule schedule = MockRepository.GenerateStrictMock <ISchedule>(); VoyageState state = MockRepository.GeneratePartialMock <VoyageState>(number, schedule); VoyageEventArgs eventArguments = null; IVoyage eventSender = null; // act: IVoyage voyage = MockRepository.GeneratePartialMock <Challenge00.DDDSample.Voyage.Voyage>(state); voyage.Departed += delegate(object sender, VoyageEventArgs e) { eventArguments = e; eventSender = sender as IVoyage; }; Assert.Throws <ArgumentNullException>(delegate { voyage.DepartFrom(null); }); // assert: Assert.IsNull(eventSender); Assert.IsNull(eventArguments); state.VerifyAllExpectations(); schedule.VerifyAllExpectations(); }