Created
April 24, 2019 19:50
-
-
Save philippdolder/23dfc84b983423544b93210d069daeb8 to your computer and use it in GitHub Desktop.
FluentAssertions own extensions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 Thank you, works now. As I understood the documentation, this appeared to be only necessary if using Should()
inside of custom assertions.
Hmm, that was not the intention.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You need to add the
[CustomAssertion]
attribute. See https://fluentassertions.com/documentation/