public void GetRecordingTarget_TargetThrows_InvocationWriterCalled() { // Arrange var writerMock = new Mock <IInvocationWriter>(); var recorder = new Recorder(writerMock.Object); var targetMock = new Mock <IFakeTarget>(); var expectedException = new FakeTargetException(); targetMock .Setup(x => x.GetRemoteResource()) .Throws(expectedException); var targetUnderRecording = recorder.GetRecordingTarget <IFakeTarget>(targetMock.Object); // Act ExceptionAssert.Throws <FakeTargetException>(() => targetUnderRecording.GetRemoteResource()); // Assert writerMock.Verify( x => x.WriteInvocation <IFakeTarget>( It.Is <MethodInfo>(method => method.Name.Equals("GetRemoteResource")), It.Is <InvocationResult>(returnValue => returnValue is ExceptionInvocationResult && (returnValue as ExceptionInvocationResult).ThrownException == expectedException), It.Is <object[]>(arguments => arguments.Length == 0)), Times.Once); }
public async Task GetRecordingTarget_ReturnValueIsTask_TargetThrows_InvocationWriterCalled() { // Arrange var writerMock = new Mock <IInvocationWriter>(); var recorder = new Recorder(writerMock.Object); var targetMock = new Mock <IFakeTargetAsync>(); var expectedException = new FakeTargetException(); targetMock .Setup(x => x.CallRemoteResourceAsync()) .Returns(Task.FromException(expectedException)); var targetUnderRecording = recorder.GetRecordingTarget <IFakeTargetAsync>(targetMock.Object); // Act await ExceptionAssert.ThrowsAsync <FakeTargetException>( async() => await targetUnderRecording.CallRemoteResourceAsync()); // Assert writerMock.Verify( x => x.WriteInvocation <IFakeTargetAsync>( It.Is <MethodInfo>(method => method.Name.Equals("CallRemoteResourceAsync")), It.Is <InvocationResult>(returnValue => returnValue is ExceptionInvocationResult && (returnValue as ExceptionInvocationResult).ThrownException == expectedException), It.Is <object[]>(arguments => arguments.Length == 0)), Times.Once); }