Assert AggregateExceptions with multiple InnerExceptions #1799
-
In the official documentation I could find the information, that With my current syntax, I would rather think that I would assert an Is there a more descriptive syntax in Version 6.2.0 (or above) to assert (await FluentActions.Awaiting(() => _myGateway.LoadAsync())
.Should()
.ThrowExactlyAsync<InvalidOperationException>())
.WithMessage("Request to API failed.")
.WithInnerException<InvalidOperationException>()
.WithMessage(expectedMessage); In my opinion, the syntax was more readable in version 5.4.1. ((Func<Task>)(async () => await _myGateway.LoadAsync()))
.Should()
.ThrowExactly<AggregateException>()
.WithInnerException<InvalidOperationException>()
.WithMessage("Request to API failed.")
.WithInnerException<InvalidOperationException>()
.WithMessage(expectedMessage); Is there a similar notation for this assertion in version 6.2.0 as in version 5.4.1? |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 2 replies
-
What stops you from writing: Func<Task> act = () => _myGateway.LoadAsync();
await act.Should().ThrowExactlyAsync<InvalidOperationException>()
.WithnnerException<InvalidOperation>()
.WithMessage("Request to API failed.")
.WithInnerException<InvalidOperationException>()
.WithMessage(expectedMessage); |
Beta Was this translation helpful? Give feedback.
-
Sorry, I gave an missleading example... public class FluentTest
{
public async Task ProduceExceptionAsync()
{
Task task1 = Task.Run(() => throw new InvalidOperationException("1"));
Task task2 = Task.Run(() => throw new InvalidOperationException("2"));
await Task.WhenAll(task1, task2);
}
public async Task ProduceExceptionAsync2()
{
Task task1 = Task.Run(() => throw new InvalidOperationException("1"));
Task task2 = Task.Run(() => throw new InvalidOperationException("2"));
Task combinedTasks = Task.WhenAll(task1, task2);
try
{
await combinedTasks;
}
catch (Exception)
{
if (combinedTasks.Exception != null)
throw combinedTasks.Exception;
throw;
}
}
public void ProduceException()
{
List<string> list = new List<string>
{
"1",
"2"
};
Parallel.ForEach(list, entry => throw new InvalidOperationException(entry));
}
public void ProduceException2()
{
throw new AggregateException(new List<Exception>
{
new InvalidOperationException("1"),
new InvalidOperationException("1")
});
}
}
[TestClass]
public class FluentTestBehaviour
{
private FluentTest _fluentTest;
[TestInitialize]
public void Initialize()
{
_fluentTest = new FluentTest();
}
[TestMethod]
public async Task ProduceExceptionAsync_Should_ThrowAnInvalidOperationException()
{
//Working
//act && assert
await FluentActions.Awaiting(() => _fluentTest.ProduceExceptionAsync())
.Should()
.ThrowExactlyAsync<InvalidOperationException>()
.WithMessage("1");
}
[TestMethod]
public async Task ProduceExceptionAsync2_Should_ThrowAnAggregateExceptionWithTwoInvalidOperationExceptions()
{
//Not Working
//act && assert
(await FluentActions.Awaiting(() => _fluentTest.ProduceExceptionAsync2())
.Should()
.ThrowExactlyAsync<AggregateException>())
.WithInnerException<InvalidOperationException>()
.WithMessage("1")
.WithInnerException<InvalidOperationException>()
.WithMessage("2"); // How can I assert InvalidOperationException with Message 2;
}
[TestMethod]
public void ProduceException_Should_ThrowAnInvalidOperationException()
{
//Working
//act && assert
FluentActions.Invoking(() => _fluentTest.ProduceException())
.Should().ThrowExactly<AggregateException>()
.WithInnerException<InvalidOperationException>();
}
[TestMethod]
public void ProduceException2_Should_ThrowAnInvalidOperationException()
{
//Not Working
//act && assert
FluentActions.Invoking(() => _fluentTest.ProduceException2())
.Should().ThrowExactly<AggregateException>()
.WithInnerException<InvalidOperationException>()
.WithMessage("1")
.WithInnerException<InvalidOperationException>()
.WithMessage("2"); // How can I assert InvalidOperationException with Message 2
}
} |
Beta Was this translation helpful? Give feedback.
-
The below verifies that the first FluentActions.Invoking(() => _fluentTest.ProduceException2())
.Should().ThrowExactly<AggregateException>()
.WithInnerException<InvalidOperationException>()
.WithMessage("1")
.WithInnerException<InvalidOperationException>()
.WithMessage("2"); // How can I assert InvalidOperationException with Message 2 Instead use this: FluentActions.Invoking(() => _fluentTest.ProduceException2())
.Should().ThrowExactly<AggregateException>()
.WithInnerException<InvalidOperationException>()
.WithMessage("1");
FluentActions.Invoking(() => _fluentTest.ProduceException2())
.Should().ThrowExactly<AggregateException>()
.WithInnerException<InvalidOperationException>()
.WithMessage("2"); Or, since FluentActions.Invoking(() => _fluentTest.ProduceException2())
.Should().Throw<InvalidOperationException>()
.WithMessage("1");
FluentActions.Invoking(() => _fluentTest.ProduceException2())
.Should().Throw<InvalidOperationException>()
.WithMessage("2"); |
Beta Was this translation helpful? Give feedback.
-
That's because |
Beta Was this translation helpful? Give feedback.
That's because
ProduceException2
throws two instances ofInvalidOperationException
whichMessage
is1