Openness for Caller Argument Expression in Assert failure messages
See original GitHub issueDescription
With .Net 6 Caller Argument Expression was added, this could be used in testfx to provide the expression which failed the check. Doing this could provider consumers more out of the box detail about what caused the assert to fail without having to manually provide messages.
Sample Code change
#if !NET6_0_OR_GREATER
public static void IsNull(object value)
{
IsNull(value, string.Empty, null);
}
#endif
public static void IsNull(
object value,
#if !NET6_0_OR_GREATER
string message)
#else
[CallerArgumentExpression("value")]string message = "")
#endif
{
IsNull(value, message, null);
}
Work required
- Add .Net 6 as a built/packaged runtime
- Modify Assert code to make the flows without a message removed in .Net 6 so that code like
Assert.IsNull(myParam)
execute passingmyParam
as the message.
Change in behavior
Currently a failing call to Assert.IsNull without a message will just return Assert.IsNull failed.
This would change to Assert.IsNull failed. myParam
which could be considered a breaking change and a result a no-go.
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:7 (5 by maintainers)
Top Results From Across the Web
Caller argument expression - C# 10.0 draft feature ...
This feature specification describes attributes that can be used to enable the compiler to convert a caller argument expression into text.
Read more >Caller Argument Expressions – A Look at New Language ...
In this post we will discuss all relevant aspects of Caller Argument Expressions and where they're helping. Have fun and be amazed!
Read more >CallerArgumentExpression Attribute In C# 10
Let's write a simple Assert method using the CallerArgumentExpression attribute and use it in the Exists method. void Assert(bool condition, [ ...
Read more >C# Make Assert Print Expression Being Asserted
I am just transitioning to C# but in C++ this could be done using a macro like this: #define ASSERT(Expr) Debug.Assert(Expr, "Assertion "...
Read more >How C# 10.0 and .NET 6.0 improve ArgumentExceptions
Assert method being used here just takes a bool , so at runtime all it can do is tell you that something failed;...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Ahh I see, thanks for the clarification
If your method has some argument marked with the
CallerArgumentExpression
attribute there is no problem for the method to be tested. The goal of this ticket is to rewrite MSTest assertions methods to be using this attribute so that if the user provides no custom message we can provide a better default message (being the caller expression).