public void Recieve_atALocation_dontBlockExceptionsFromCurrentState() { // arrange: GList mocks = new GList(); DateTime date = DateTime.Now + TimeSpan.FromDays(1); Exception eThrown = new Exception("Catch me."); TrackingId identifier = new TrackingId("CARGO01"); ILocation location = MockRepository.GenerateStrictMock<ILocation>(); mocks.Add(location); IRouteSpecification route = MockRepository.GenerateStrictMock<IRouteSpecification>(); mocks.Add(route); CargoState mockState1 = MockRepository.GeneratePartialMock<CargoState>(identifier, route); mockState1.Expect(s => s.Recieve(location, date)).Throw(eThrown); mocks.Add(mockState1); HandlingEventArgs eventArguments = null; ICargo eventSender = null; // act: TCargo underTest = new FakeCargo(mockState1); underTest.Recieved += delegate(object sender, HandlingEventArgs e) { eventArguments = e; eventSender = sender as ICargo; }; Assert.Throws<Exception>(delegate { underTest.Recieve(location, date); }, "Catch me."); // assert: Assert.IsNull(eventArguments); Assert.IsNull(eventSender); foreach(object mock in mocks) mock.VerifyAllExpectations(); }
public void Recieve_atALocation_delegateLogicToCurrentState() { // arrange: GList mocks = new GList(); TrackingId identifier = new TrackingId("CARGO01"); DateTime date = DateTime.Now + TimeSpan.FromDays(1); UnLocode unlocode = new UnLocode("RECLC"); ILocation location = MockRepository.GenerateStrictMock<ILocation>(); mocks.Add(location); IRouteSpecification route = MockRepository.GenerateStrictMock<IRouteSpecification>(); mocks.Add(route); IRouteSpecification route2 = MockRepository.GenerateStrictMock<IRouteSpecification>(); mocks.Add(route2); CargoState mockState1 = MockRepository.GeneratePartialMock<CargoState>(identifier, route); CargoState mockState2 = MockRepository.GeneratePartialMock<CargoState>(mockState1, route2); mockState1.Expect(s => s.Recieve(location, date)).Return(mockState2).Repeat.Once(); mockState1.Expect(s => s.Equals(mockState2)).Return(false).Repeat.Any(); mockState2.Expect(s => s.LastKnownLocation).Return(unlocode).Repeat.AtLeastOnce(); mocks.Add(mockState1); mocks.Add(mockState2); HandlingEventArgs eventArguments = null; ICargo eventSender = null; // act: TCargo underTest = new FakeCargo(mockState1); underTest.Recieved += delegate(object sender, HandlingEventArgs e) { eventArguments = e; eventSender = sender as ICargo; }; underTest.Recieve(location, date); // assert: Assert.AreSame(unlocode, underTest.Delivery.LastKnownLocation); Assert.IsNotNull(eventArguments); Assert.AreSame(unlocode, eventArguments.Delivery.LastKnownLocation); Assert.AreSame(underTest, eventSender); foreach(object mock in mocks) mock.VerifyAllExpectations(); }