Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public async Task Fails_For_Different_Parameter_Name()
var paramName1 = "foo";
var paramName2 = "bar";
var expectedMessage = """
Expected ArgumentException to have parameter name "bar"
Expected ArgumentException to have parameter name "bar" (exact type)
but ArgumentException parameter name was "foo"

at Assert.That(action).ThrowsExactly<ArgumentException>().WithParameterName("bar")
Expand Down
16 changes: 14 additions & 2 deletions TUnit.Assertions/Conditions/ExceptionPropertyAssertions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -267,13 +267,16 @@ public class ExceptionParameterNameAssertion<TException> : Assertion<TException>
where TException : Exception
{
private readonly string _expectedParameterName;
private readonly bool _requireExactType;

public ExceptionParameterNameAssertion(
AssertionContext<TException> context,
string expectedParameterName)
string expectedParameterName,
bool requireExactType = false)
: base(context)
{
_expectedParameterName = expectedParameterName;
_requireExactType = requireExactType;
}

protected override Task<AssertionResult> CheckAsync(EvaluationMetadata<TException> metadata)
Expand All @@ -291,6 +294,13 @@ protected override Task<AssertionResult> CheckAsync(EvaluationMetadata<TExceptio
return Task.FromResult(AssertionResult.Failed("no exception was thrown"));
}

// If exact type is required, check that first
if (_requireExactType && exception.GetType() != typeof(TException))
{
return Task.FromResult(AssertionResult.Failed(
$"wrong exception type: {exception.GetType().Name} instead of exactly {typeof(TException).Name}"));
}

if (exception is ArgumentException argumentException)
{
if (argumentException.ParamName == _expectedParameterName)
Expand All @@ -307,5 +317,7 @@ protected override Task<AssertionResult> CheckAsync(EvaluationMetadata<TExceptio
}

protected override string GetExpectation() =>
$"ArgumentException to have parameter name \"{_expectedParameterName}\"";
_requireExactType
? $"{typeof(TException).Name} to have parameter name \"{_expectedParameterName}\" (exact type)"
: $"ArgumentException to have parameter name \"{_expectedParameterName}\"";
}
2 changes: 1 addition & 1 deletion TUnit.Assertions/Conditions/ThrowsAssertion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ public ExceptionMessageMatchesAssertion<TException> WithMessageMatching(StringMa
public ExceptionParameterNameAssertion<TException> WithParameterName(string expectedParameterName)
{
Context.ExpressionBuilder.Append($".WithParameterName(\"{expectedParameterName}\")");
return new ExceptionParameterNameAssertion<TException>(Context, expectedParameterName);
return new ExceptionParameterNameAssertion<TException>(Context, expectedParameterName, requireExactType: true);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ namespace .Conditions
public class ExceptionParameterNameAssertion<TException> : .<TException>
where TException :
{
public ExceptionParameterNameAssertion(.<TException> context, string expectedParameterName) { }
public ExceptionParameterNameAssertion(.<TException> context, string expectedParameterName, bool requireExactType = false) { }
protected override .<.> CheckAsync(.<TException> metadata) { }
protected override string GetExpectation() { }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ namespace .Conditions
public class ExceptionParameterNameAssertion<TException> : .<TException>
where TException :
{
public ExceptionParameterNameAssertion(.<TException> context, string expectedParameterName) { }
public ExceptionParameterNameAssertion(.<TException> context, string expectedParameterName, bool requireExactType = false) { }
protected override .<.> CheckAsync(.<TException> metadata) { }
protected override string GetExpectation() { }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ namespace .Conditions
public class ExceptionParameterNameAssertion<TException> : .<TException>
where TException :
{
public ExceptionParameterNameAssertion(.<TException> context, string expectedParameterName) { }
public ExceptionParameterNameAssertion(.<TException> context, string expectedParameterName, bool requireExactType = false) { }
protected override .<.> CheckAsync(.<TException> metadata) { }
protected override string GetExpectation() { }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ namespace .Conditions
public class ExceptionParameterNameAssertion<TException> : .<TException>
where TException :
{
public ExceptionParameterNameAssertion(.<TException> context, string expectedParameterName) { }
public ExceptionParameterNameAssertion(.<TException> context, string expectedParameterName, bool requireExactType = false) { }
protected override .<.> CheckAsync(.<TException> metadata) { }
protected override string GetExpectation() { }
}
Expand Down
Loading