Skip to content

Commit

Permalink
Improve readability of assertion failure messages (#838)
Browse files Browse the repository at this point in the history
* Move BecauseTests to separate project using TUnit as Framework

* Simplify some messages

* Simplify remaining messages

* Fix implementation for AND/OR conditions

* Include Expression in message

* Fix failing tests

* Rename `Passes` to `GetResult`

* Finish sentence with a dot

* Implement review comments

* Use spaces instead of tabs
  • Loading branch information
vbreuss authored Oct 10, 2024
1 parent 6cf0b36 commit 888c4f7
Show file tree
Hide file tree
Showing 81 changed files with 2,208 additions and 1,757 deletions.
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()
{
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

0 comments on commit 888c4f7

Please sign in to comment.