Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 0 additions & 80 deletions 80 Tests/FluentAssertions.Specs/Exceptions/ExceptionAssertionSpecs.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using Xunit;
using Xunit.Sdk;

Expand Down Expand Up @@ -150,82 +149,3 @@ public void ThrowExactly_when_subject_throws_expected_exception_it_should_not_do
act.Should().ThrowExactly<ArgumentNullException>();
}
}

public class SomeTestClass
{
internal const string ExceptionMessage = "someMessage";

public IList<string> Strings = new List<string>();

public void Throw()
{
throw new ArgumentException(ExceptionMessage);
}
}

public abstract class Does
{
public abstract void Do();

public abstract void Do(string someParam);

public abstract int Return();

public static Does Throw<TException>(TException exception)
where TException : Exception
{
return new DoesThrow<TException>(exception);
}

public static Does Throw<TException>()
where TException : Exception, new()
{
return Throw(new TException());
}

public static Does NotThrow() => new DoesNotThrow();

private class DoesThrow<TException> : Does
where TException : Exception
{
private readonly TException exception;

public DoesThrow(TException exception)
{
this.exception = exception;
}

public override void Do() => throw exception;

public override void Do(string someParam) => throw exception;

public override int Return() => throw exception;
}

private class DoesNotThrow : Does
{
public override void Do() { }

public override void Do(string someParam) { }

public override int Return() => 42;
}
}

internal class ExceptionWithProperties : Exception
{
public ExceptionWithProperties(string propertyValue)
{
Property = propertyValue;
}

public string Property { get; set; }
}

internal class ExceptionWithEmptyToString : Exception
{
public override string ToString()
{
return string.Empty;
}
}
26 changes: 11 additions & 15 deletions 26 Tests/FluentAssertions.Specs/Exceptions/InnerExceptionSpecs.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using Xunit;
using Xunit.Sdk;

Expand All @@ -10,11 +10,10 @@ public class InnerExceptionSpecs
public void When_subject_throws_an_exception_with_the_expected_inner_exception_it_should_not_do_anything()
{
// Arrange
Does testSubject = Does.Throw(new Exception("", new ArgumentException()));
Action testSubject = () => throw new Exception("", new ArgumentException());

// Act / Assert
testSubject
.Invoking(x => x.Do())
.Should().Throw<Exception>()
.WithInnerException<ArgumentException>();
}
Expand All @@ -23,11 +22,10 @@ public void When_subject_throws_an_exception_with_the_expected_inner_exception_i
public void When_subject_throws_an_exception_with_the_expected_inner_base_exception_it_should_not_do_anything()
{
// Arrange
Does testSubject = Does.Throw(new Exception("", new ArgumentNullException()));
Action testSubject = () => throw new Exception("", new ArgumentNullException());

// Act / Assert
testSubject
.Invoking(x => x.Do())
.Should().Throw<Exception>()
.WithInnerException<ArgumentException>();
}
Expand All @@ -36,11 +34,10 @@ public void When_subject_throws_an_exception_with_the_expected_inner_base_except
public void When_subject_throws_an_exception_with_the_expected_inner_exception_from_argument_it_should_not_do_anything()
{
// Arrange
Does testSubject = Does.Throw(new Exception("", new ArgumentException()));
Action testSubject = () => throw new Exception("", new ArgumentException());

// Act / Assert
testSubject
.Invoking(x => x.Do())
.Should().Throw<Exception>()
.WithInnerException(typeof(ArgumentException));
}
Expand Down Expand Up @@ -186,23 +183,22 @@ public void When_subject_throws_an_exception_with_an_unexpected_inner_exception_
// Arrange
var innerException = new NullReferenceException("InnerExceptionMessage");

Does testSubject = Does.Throw(new Exception("", innerException));
Action testSubject = () => throw new Exception("", innerException);

try
{
// Act
testSubject
.Invoking(x => x.Do())
.Should().Throw<Exception>()
.WithInnerException<ArgumentException>("because {0} should do just that", "Does.Do");
.WithInnerException<ArgumentException>("because {0} should do just that", "the action");

throw new XunitException("This point should not be reached");
}
catch (XunitException exc)
{
// Assert
exc.Message.Should().Match(
"Expected*ArgumentException*Does.Do should do just that*NullReferenceException*InnerExceptionMessage*");
"Expected*ArgumentException*the action should do just that*NullReferenceException*InnerExceptionMessage*");
}
}

Expand All @@ -211,17 +207,17 @@ public void When_subject_throws_an_exception_without_expected_inner_exception_it
{
try
{
Does testSubject = Does.Throw<Exception>();
Action testSubject = () => throw new Exception();

testSubject.Invoking(x => x.Do()).Should().Throw<Exception>()
.WithInnerException<InvalidOperationException>("because {0} should do that", "Does.Do");
testSubject.Should().Throw<Exception>()
.WithInnerException<InvalidOperationException>("because {0} should do that", "the action");

throw new XunitException("This point should not be reached");
}
catch (XunitException ex)
{
ex.Message.Should().Be(
"Expected inner System.InvalidOperationException because Does.Do should do that, but the thrown exception has no inner exception.");
"Expected inner System.InvalidOperationException because the action should do that, but the thrown exception has no inner exception.");
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using Xunit;

namespace FluentAssertions.Specs.Exceptions;
Expand All @@ -9,10 +9,10 @@ public class InvokingActionSpecs
public void Invoking_on_null_is_not_allowed()
{
// Arrange
Does someClass = null;
Action someClass = null;

// Act
Action act = () => someClass.Invoking(d => d.Do());
Action act = () => someClass.Invoking(d => d());

// Assert
act.Should().ThrowExactly<ArgumentNullException>()
Expand All @@ -23,7 +23,7 @@ public void Invoking_on_null_is_not_allowed()
public void Invoking_with_null_is_not_allowed()
{
// Arrange
Does someClass = Does.NotThrow();
Action someClass = () => { };

// Act
Action act = () => someClass.Invoking(null);
Expand Down
10 changes: 5 additions & 5 deletions 10 Tests/FluentAssertions.Specs/Exceptions/InvokingFunctionSpecs.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using Xunit;

namespace FluentAssertions.Specs.Exceptions;
Expand All @@ -9,10 +9,10 @@ public class InvokingFunctionSpecs
public void Invoking_on_null_is_not_allowed()
{
// Arrange
Does someClass = null;
Func<int> someClass = null;

// Act
Action act = () => someClass.Invoking(d => d.Return());
Action act = () => someClass.Invoking(d => d());

// Assert
act.Should().ThrowExactly<ArgumentNullException>()
Expand All @@ -23,10 +23,10 @@ public void Invoking_on_null_is_not_allowed()
public void Invoking_with_null_is_not_allowed()
{
// Arrange
Does someClass = Does.NotThrow();
Action someClass = () => { };

// Act
Action act = () => someClass.Invoking((Func<Does, object>)null);
Action act = () => someClass.Invoking((Func<Action, object>)null);

// Assert
act.Should().ThrowExactly<ArgumentNullException>()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,53 @@
using System;
using System;
using System.Collections.Generic;
using Xunit;
using Xunit.Sdk;

namespace FluentAssertions.Specs.Exceptions;

internal class ExceptionWithEmptyToString : Exception
{
public override string ToString()
{
return string.Empty;
}
}

internal class ExceptionWithProperties : Exception
{
public ExceptionWithProperties(string propertyValue)
{
Property = propertyValue;
}

public string Property { get; set; }
}

public class MiscellaneousExceptionSpecs
{
[Fact]
public void When_getting_value_of_property_of_thrown_exception_it_should_return_value_of_property()
{
// Arrange
const string SomeParamNameValue = "param";
Does target = Does.Throw(new ExceptionWithProperties(SomeParamNameValue));
const string someParamNameValue = "param";
Action target = () => throw new ExceptionWithProperties(someParamNameValue);

// Act
Action act = target.Do;
Action act = target;

// Assert
act.Should().Throw<ExceptionWithProperties>().And.Property.Should().Be(SomeParamNameValue);
act.Should().Throw<ExceptionWithProperties>().And.Property.Should().Be(someParamNameValue);
}

[Fact]
public void When_validating_a_subject_against_multiple_conditions_it_should_support_chaining()
{
// Arrange
Does testSubject = Does.Throw(new InvalidOperationException("message", new ArgumentException("inner message")));
Action testSubject = () => throw new InvalidOperationException("message", new ArgumentException("inner message"));

// Act / Assert
testSubject
.Invoking(x => x.Do())

.Should().Throw<InvalidOperationException>()
.WithInnerException<ArgumentException>()
.WithMessage("inner message");
Expand Down Expand Up @@ -126,8 +144,8 @@ public void When_custom_condition_is_met_it_should_not_throw()
public void When_two_exceptions_are_thrown_and_the_assertion_assumes_there_can_only_be_one_it_should_fail()
{
// Arrange
Does testSubject = Does.Throw(new AggregateException(new Exception(), new Exception()));
Action throwingMethod = testSubject.Do;
Action testSubject = () => throw new AggregateException(new Exception(), new Exception());
Action throwingMethod = testSubject;

// Act
Action action = () => throwingMethod.Should().Throw<Exception>().And.Message.Should();
Expand Down
18 changes: 9 additions & 9 deletions 18 Tests/FluentAssertions.Specs/Exceptions/NotThrowSpecs.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using FluentAssertions.Execution;
#if NET47
using FluentAssertions.Specs.Common;
Expand Down Expand Up @@ -34,11 +34,11 @@ public void When_subject_is_null_when_an_exception_should_not_be_thrown_it_shoul
public void When_a_specific_exception_should_not_be_thrown_but_it_was_it_should_throw()
{
// Arrange
Does foo = Does.Throw(new ArgumentException("An exception was forced"));
Action foo = () => throw new ArgumentException("An exception was forced");

// Act
Action action =
() => foo.Invoking(f => f.Do()).Should().NotThrow<ArgumentException>("we passed valid arguments");
() => foo.Should().NotThrow<ArgumentException>("we passed valid arguments");

// Assert
action
Expand All @@ -51,20 +51,20 @@ public void When_a_specific_exception_should_not_be_thrown_but_it_was_it_should_
public void When_a_specific_exception_should_not_be_thrown_but_another_was_it_should_succeed()
{
// Arrange
Does foo = Does.Throw<ArgumentException>();
Action foo = () => throw new ArgumentException();

// Act / Assert
foo.Invoking(f => f.Do()).Should().NotThrow<InvalidOperationException>();
foo.Should().NotThrow<InvalidOperationException>();
}

[Fact]
public void When_no_exception_should_be_thrown_but_it_was_it_should_throw()
{
// Arrange
Does foo = Does.Throw(new ArgumentException("An exception was forced"));
Action foo = () => throw new ArgumentException("An exception was forced");

// Act
Action action = () => foo.Invoking(f => f.Do()).Should().NotThrow("we passed valid arguments");
Action action = () => foo.Should().NotThrow("we passed valid arguments");

// Assert
action
Expand All @@ -77,10 +77,10 @@ public void When_no_exception_should_be_thrown_but_it_was_it_should_throw()
public void When_no_exception_should_be_thrown_and_none_was_it_should_not_throw()
{
// Arrange
Does foo = Does.NotThrow();
Action foo = () => { };

// Act / Assert
foo.Invoking(f => f.Do()).Should().NotThrow();
foo.Should().NotThrow();
}

[Fact]
Expand Down
Loading
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.