public void should_send_notifications_after_the_task_is_executed_even_if_exception_occurs() { var task = Task.New<ExecuteTask>(); var taskWithAttributesThatThrows = new MockTaskThatThrowsWithAttributes(); task.In.Task = taskWithAttributesThatThrows; var mockInvocation = new Mock<IInvocation>(); mockInvocation.Setup(invocation => invocation.Proceed()).Callback(taskWithAttributesThatThrows.Execute); task.In.Invocation = mockInvocation.Object; var throwHappened = false; try { try { task.Execute(); } finally { VerifySevenCallbacks(taskWithAttributesThatThrows); } } catch (MockException) { throwHappened = true; } Assert.That(throwHappened, "The exception should still happen."); }
public void should_send_notifications_if_task_execution_throws_an_unhandled_exception() { var task = Task.New<ExecuteTask>(); var taskWithAttributesThatThrows = new MockTaskThatThrowsWithAttributes(); task.In.Task = taskWithAttributesThatThrows; var mockInvocation = new Mock<IInvocation>(); mockInvocation.Setup(invocation => invocation.Proceed()).Callback(taskWithAttributesThatThrows.Execute); task.In.Invocation = mockInvocation.Object; Assert.Throws(typeof(MockException), task.Execute); VerifySevenCallbacks(taskWithAttributesThatThrows); }
static void VerifySevenCallbacks(MockTaskThatThrowsWithAttributes taskThatThrowsWithAttributes) { Assert.That(taskThatThrowsWithAttributes.CallbackQueue.Count, Is.EqualTo(7)); // Note: Attributes on a class are not returned in any order and therefore the it can not be assumed that the // first attribute will receive the first callback. if (taskThatThrowsWithAttributes.CallbackQueue.Peek().Contains("First")) { Assert.That(taskThatThrowsWithAttributes.CallbackQueue.Dequeue(), Is.EqualTo("First.Before")); Assert.That(taskThatThrowsWithAttributes.CallbackQueue.Dequeue(), Is.EqualTo("Second.Before")); Assert.That(taskThatThrowsWithAttributes.CallbackQueue.Dequeue(), Is.EqualTo("Execute")); Assert.That(taskThatThrowsWithAttributes.CallbackQueue.Dequeue(), Is.EqualTo("Second.OnError")); Assert.That(taskThatThrowsWithAttributes.CallbackQueue.Dequeue(), Is.EqualTo("First.OnError")); Assert.That(taskThatThrowsWithAttributes.CallbackQueue.Dequeue(), Is.EqualTo("Second.After")); Assert.That(taskThatThrowsWithAttributes.CallbackQueue.Dequeue(), Is.EqualTo("First.After")); } else { Assert.That(taskThatThrowsWithAttributes.CallbackQueue.Dequeue(), Is.EqualTo("Second.Before")); Assert.That(taskThatThrowsWithAttributes.CallbackQueue.Dequeue(), Is.EqualTo("First.Before")); Assert.That(taskThatThrowsWithAttributes.CallbackQueue.Dequeue(), Is.EqualTo("Execute")); Assert.That(taskThatThrowsWithAttributes.CallbackQueue.Dequeue(), Is.EqualTo("First.OnError")); Assert.That(taskThatThrowsWithAttributes.CallbackQueue.Dequeue(), Is.EqualTo("Second.OnError")); Assert.That(taskThatThrowsWithAttributes.CallbackQueue.Dequeue(), Is.EqualTo("First.After")); Assert.That(taskThatThrowsWithAttributes.CallbackQueue.Dequeue(), Is.EqualTo("Second.After")); } }