Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve readability of assertion failure messages #838

Merged
merged 10 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from 8 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
@@ -1,6 +1,4 @@
using TUnit.Assertions.Extensions;

namespace TUnit.Assertions.UnitTests.AssertConditions;
namespace TUnit.Assertions.Tests.AssertConditions;

public class BecauseTests
{
Expand All @@ -15,14 +13,14 @@ public async Task Include_Because_Reason_In_Message()
await Assert.That(variable).IsFalse().Because(because);
};

var exception = await Assert.ThrowsAsync<TUnit.Assertions.Exceptions.AssertionException>(action);
var exception = await Assert.ThrowsAsync<AssertionException>(action);
await Assert.That(exception.Message).Contains(because);
}

[Test]
[TestCase("we prefix the reason", "Because: we prefix the reason")]
[TestCase(" we ignore whitespace", "Because: we ignore whitespace")]
[TestCase("because we honor a leading 'because'", "Because: we honor a leading 'because'")]
[Arguments("we prefix the reason", "because we prefix the reason")]
[Arguments(" we ignore whitespace", "because we ignore whitespace")]
[Arguments("because we honor a leading 'because'", "because we honor a leading 'because'")]
public async Task Prefix_Because_Message(string because, string expectedWithPrefix)
{
var variable = true;
Expand All @@ -32,7 +30,7 @@ public async Task Prefix_Because_Message(string because, string expectedWithPref
await Assert.That(variable).IsFalse().Because(because);
};

var exception = await Assert.ThrowsAsync<TUnit.Assertions.Exceptions.AssertionException>(action);
var exception = await Assert.ThrowsAsync<AssertionException>(action);
await Assert.That(exception.Message).Contains(expectedWithPrefix);
}

Expand All @@ -47,32 +45,39 @@ public async Task Honor_Already_Present_Because_Prefix()
await Assert.That(variable).IsFalse().Because(because);
};

var exception = await Assert.ThrowsAsync<TUnit.Assertions.Exceptions.AssertionException>(action);
await Assert.That(exception.Message).Contains("we honor a leading 'because'")
.And.DoesNotContain(because);
var exception = await Assert.ThrowsAsync<AssertionException>(action);
await Assert.That(exception.Message).Contains(because)
.And.DoesNotContain("because because");
}

[Test]
public async Task Without_Because_Use_Empty_String()
{
var variable = true;
string expectedMessage = """
Expected variable to be equal to False, but found True.
At Assert.That(variable).IsFalse()
""";

var variable = true;

var action = async () =>
{
await Assert.That(variable).IsFalse();
};

var exception = await Assert.ThrowsAsync<TUnit.Assertions.Exceptions.AssertionException>(action);
await Assert.That(exception.Message).IsEqualTo("""
Assert.That(variable).IsFalse()
Expected: False
Received: True
""");
var exception = await Assert.ThrowsAsync<AssertionException>(action);
await Assert.That(exception.Message).IsEqualTo(expectedMessage);
}

[Test]
public async Task Apply_Because_Reasons_Only_On_Previous_Assertions()
{
string expectedMessage = """
Expected variable to be equal to True, because we only apply it to previous assertions
and
to be equal to False, but found True.
At Assert.That(variable).IsTrue().And.IsFalse()
""";
var because = "we only apply it to previous assertions";
var variable = true;

Expand All @@ -82,12 +87,8 @@ await Assert.That(variable).IsTrue().Because(because)
.And.IsFalse();
};

var exception = await Assert.ThrowsAsync<TUnit.Assertions.Exceptions.AssertionException>(action);
await Assert.That(exception.Message).IsEqualTo("""
Assert.That(variable).IsTrue().And.IsFalse()
Expected: False
Received: True
""");
var exception = await Assert.ThrowsAsync<AssertionException>(action);
await Assert.That(exception.Message).IsEqualTo(expectedMessage);
}

[Test]
Expand All @@ -103,7 +104,7 @@ await Assert.That(variable).IsTrue().Because(because1)
.And.IsFalse().Because(because2);
};

var exception = await Assert.ThrowsAsync<TUnit.Assertions.Exceptions.AssertionException>(action);
var exception = await Assert.ThrowsAsync<AssertionException>(action);
await Assert.That(exception.Message).Contains(because1);
}

Expand All @@ -120,7 +121,7 @@ await Assert.That(variable).IsTrue().Because(because1)
.And.IsFalse().Because(because2);
};

var exception = await Assert.ThrowsAsync<TUnit.Assertions.Exceptions.AssertionException>(action);
var exception = await Assert.ThrowsAsync<AssertionException>(action);
await Assert.That(exception.Message).Contains(because2);
}

Expand All @@ -137,7 +138,7 @@ await Assert.That(variable).IsFalse().Because(because1)
.Or.IsFalse().Because(because2);
};

var exception = await Assert.ThrowsAsync<TUnit.Assertions.Exceptions.AssertionException>(action);
var exception = await Assert.ThrowsAsync<AssertionException>(action);
await Assert.That(exception.Message).Contains(because1).And.Contains(because2);
}
}
3 changes: 3 additions & 0 deletions TUnit.Assertions.Tests/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
global using TUnit.Core;
global using TUnit.Assertions.Exceptions;
global using TUnit.Assertions.Extensions;
10 changes: 10 additions & 0 deletions TUnit.Assertions.Tests/TUnit.Assertions.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\TUnit\TUnit.csproj" />
</ItemGroup>
</Project>
Loading
Loading