Skip to content

Instantly share code, notes, and snippets.

@philippdolder
Created April 24, 2019 19:50
Show Gist options
  • Save philippdolder/23dfc84b983423544b93210d069daeb8 to your computer and use it in GitHub Desktop.
Save philippdolder/23dfc84b983423544b93210d069daeb8 to your computer and use it in GitHub Desktop.
FluentAssertions own extensions
namespace Gist
{
using FluentAssertions.Execution;
using Xunit;
public class MyCustomAssertionsFacts
{
[Fact]
public void DoesNotPrintContextNicely()
{
var testee = new MyCustom(0);
testee.Should().ContainPositiveValue();
// error message is: Expected object to contain a positive value, but found 0.
// expected message is: Expected testee to contain a positive value, but found 0.
}
}
public class MyCustomAssertions
{
private readonly MyCustom subject;
public MyCustomAssertions(MyCustom subject)
{
this.subject = subject;
}
public void ContainPositiveValue()
{
Execute.Assertion.ForCondition(this.subject.Value > 0)
.FailWith("Expected {context} to contain a positive value, but found {0}.", this.subject.Value);
}
}
public static class MyCustomAssertionsExtensions
{
public static MyCustomAssertions Should(this MyCustom actualValue)
{
return new MyCustomAssertions(actualValue);
}
}
public class MyCustom
{
public MyCustom(int value)
{
this.Value = value;
}
public int Value { get; }
}
}
@dennisdoomen
Copy link

You need to add the [CustomAssertion] attribute. See https://fluentassertions.com/documentation/

@philippdolder
Copy link
Author

philippdolder commented Apr 26, 2019

@dennisdoomen Thank you, works now. As I understood the documentation, this appeared to be only necessary if using Should() inside of custom assertions.

@dennisdoomen
Copy link

Hmm, that was not the intention.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment