Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • 13:12
    jairbubbles commented #1249
  • 08:30
    dennisdoomen labeled #1690
  • 08:29
    dennisdoomen commented #1115
  • Sep 24 19:05
    michael-hawker commented #1149
  • Sep 24 15:34
    jnyrup opened #1690
  • Sep 24 14:38
    weelink commented #1422
  • Sep 24 13:29
    BEagle1984 commented #1115
  • Sep 24 13:04
    dennisdoomen commented #1115
  • Sep 24 08:18
    BEagle1984 commented #1115
  • Sep 24 07:23
    dennisdoomen commented #1115
  • Sep 24 06:46
    BEagle1984 commented #1115
  • Sep 24 06:42
    BEagle1984 commented #1115
  • Sep 24 06:00
    jnyrup commented #1115
  • Sep 24 06:00
    jnyrup commented #1115
  • Sep 24 05:29
    dennisdoomen commented #1149
  • Sep 24 05:28
    dennisdoomen commented #1115
  • Sep 23 15:18
    michael-hawker commented #1149
  • Sep 23 14:58
    BEagle1984 commented #1115
  • Sep 23 14:57
    BEagle1984 synchronize #1689
  • Sep 23 14:56
    BEagle1984 opened #1689
Pavel Vostretsov
@fakefeik

Hi
I wanted to use FluentAssertions for object comparison with some properties left out. I have TMessage actual, TMessage expected, Expression<Func<TMessage, object>>[] leftOutProperties.
I've written such code:

actualMessage.ShouldBeEquivalentTo(expectedMessage, o => 
{
    foreach(var expression in leftOutProperties)
        o.Excluding(expression);
    return o;
});

and it works like a charm. But there is a problem: instead of an exception, I want to get List<ComparisonResult> from this method, where ComparisonResult contains object Expected, object Actual and Expression PathToProperty. Is there any simple way I could accomplish this? I've read documentation and I couldn't find one.

Dennis Doomen
@dennisdoomen
@fakefeik what do you mean with these comparison results? To evaluate the differences?
Pavel Vostretsov
@fakefeik

I mean that now I use it like this:

public string Compare<TMessage>(TMessage expectedMessage, TMessage actualMessage)
{
    try
    {
        actualMessage.ShouldBeEquivalentTo(expectedMessage, o =>
        {
            foreach (var expression in leftOutProperties)
                o.Excluding(expression);
            return o;
        });
        return null;
    }
    catch (AssertionException e)
    {
        return e.Message;
    }
}

What I want to be able to do is get rid of the try and return not a string, but some list of comparison results for pretty-printing it later, like this

public IEnumerable<ComparisonResult> Compare<TMessage>(TMessage expectedMessage, TMessage actualMessage)
{
    return actualMessage.ShouldBeEquivalentTo(expectedMessage, o =>
    {
        foreach (var expression in anyValuePaths)
            o.Excluding(expression);
        return o;
    });
}
I've looked through the code and it seems the easiest way is to write an extension method that would behave similar to ShouldBeEquivalentTo, but would use different AssertionStrategy
Although it seems to be quite difficult because there is no public constructor that takes AssertionStrategy as a parameter
Pavel Vostretsov
@fakefeik
Perhaps, there's something I'm missing?
Dennis Doomen
@dennisdoomen
No, not out of the box. What that requires is that the [AssertionScope] (https://github.com/dennisdoomen/FluentAssertions/blob/master/Src/Core/Execution/AssertionScope.cs) constructor supports an ambient assertion scope. I would welcome a PR ;-)
Pavel Vostretsov
@fakefeik
I don't quite get it what is an ambient assertion scope
though I think it is enough to make Dispose() virtual and assertionStrategy and contextData protected in AssertionScope
So I could create my own MyAssertionScope that inherits AssertionScope, override Dispose() and use it
Dennis Doomen
@dennisdoomen
The AssertionScope class uses a thread-static field to track the currently executing assertion to collect and throw them as one assertion exception. By creating an instance of that class, it assigns a new CollectingAssertionStrategy to that field. So we have two options to accomplish what you're asking for:
  1. Allow multiple assertion scopes on the current thread to share the collection strategy, but this would require some kind of reference counting
  2. Extend the ShouldBeEquivalentTo API to allow you to pass an alternative impleemntating of the IAssertionStrategy.
Dennis Doomen
@ddoomen_twitter
@onovotny @smudge202 I'm planning the work for Fluent Assertions 5.0. Any thoughts should go dennisdoomen/FluentAssertions#463
Saro Taşciyan
@SaroTasciyan
Heya Dennis, I was planning to take some time on up for grab issues on the following weeks. If you think there is anything I can help you with v5 please let me know
Dennis Doomen
@dennisdoomen
@SaroTasciyan don't let my plans for v5 hold you back. I won't be starting this before the end of September anyway. So if you find the time to pick up any of the up-for-grabs items, that would be awesome.
Saro Taşciyan
@SaroTasciyan
Good morning Dennis, it's great that we have plenty of time to improve stuff before the major release. Just wanted to let you know I would love to get involved in v5 as well ;)
Dennis Doomen
@dennisdoomen
Sure. By the time I'm going to start with that, I'll probably create a task list in the issue and a branch on the repo.
cmellongoempyrean
@cmellongoempyrean
As far as I can tell ExcludeNestedObjects() just plain doesn't work, am I crazy?
cmellongoempyrean
@cmellongoempyrean
cmellongoempyrean
@cmellongoempyrean
amusingly, dotnetfiddle is failing to escape things properly and FluentAssertions NuGet description breaks loading from an external link. https://gist.github.com/cmellongoempyrean/bdb3c63a3755d7f27c0751919cbc93ea
Dennis Doomen
@ddoomen_twitter
@cmellongoempyrean the truth is somewhere in the middle. The only thing ExcludingNestedObjects does it to prevent traversing into your Nested property. It will still try to compare the property values as-is. And since they are not the same objects, it'll report a difference
If you just want to exclude the property, use Excluding(x => x.Nested)
cmellongoempyrean
@cmellongoempyrean
that seems deceptively named then :\
Dennis Doomen
@ddoomen_twitter
Agreed.
Christian Palmstierna
@cpx86
Question: has the idea of support for soft assertions in FluentAssertions come up before?
Dennis Doomen
@dennisdoomen
soft assertions?? Please elaborate
Christian Palmstierna
@cpx86
It's when you have multiple assertions and if one fails, the rest are still evaluated.
I know some people argue that a test should always have just a single assertion but I'd argue that isn't always feasible, e.g. when writing integration tests.
Saro Taşciyan
@SaroTasciyan
Added IsAssignableFrom to RelectionExtensions, quite confused tho. @dennisdoomen looking forward for your review (:
Dennis Doomen
@ddoomen_twitter
@cpx You can achieve the same with the AssertionScope. See the EquivalencyValidator class or the AssertionScopeSpecs for examples
@SaroTasciyan Will check the PR. Thanks for the effort regardless.
Christian Palmstierna
@cpx86
@ddoomen_twitter
Will check that out. Thx!
Pavel Vostretsov
@fakefeik

Hi

I was trying to use ShouldBeEquivalentTo and excluding specific array elements from comparison like this

public class A
{
    public B[] Arr { get; set; }
}

public class B
{
    public string A { get; set; }
    public int C { get; set; }
}

public class Test
{
    [Test]
    public void FluentAssertionsTest()
    {
        var a = new A
        {
            Arr = new[]
            {
                new B {A = "ab", C = 1},
                new B {A = "def", C = 2}
            }
        };
        var b = new A
        {
            Arr = new[]
            {
                new B {A = "abc", C = 1},
                new B {A = "def", C = 2}
            }
        };
        a.ShouldBeEquivalentTo(b, o => o.Excluding(x => x.Arr[0]));
    }
}

And it tells me
Expected member Arr[0].A to be "abc" with a length of 3, but "ab" has a length of 2.

Any chance this will be fixed in the future?

Florian Foligowski
@subprime
Hello. Can somebody tell me if i can use this libary for ASP.NET Core web projects?
Adam Rodger
@adamrodger
I'm trying to get a collection assertion to display the failure message including line breaks when the collections being compared are either very large or contain items with very long strings representations. For example:
[Test]
public void CompareCollections()
{
    var one = new[]
    {
        "ReallyLongString",
        "AnotherReallyLongString",
        "YetAnotherOne",
        "ThisIsGettingPrettyLongNow"
    };
    var two = new[]
    {
        "ButThisOneIsDifferent",
        "SoTheAssertionIsGoingToFail",
        "ButTheOutputIsReallyHardToRead",
        "BecauseItsAllOnOneLine",
        "ButItWouldBeNicerIfItWrapped"
    };

    one.Should().Equal(two);
}
This outputs: Expected collection to be equal to {"ButThisOneIsDifferent", "SoTheAssertionIsGoingToFail", "ButTheOutputIsReallyHardToRead", "BecauseItsAllOnOneLine", "ButItWouldBeNicerIfItWrapped"}, but {"ReallyLongString", "AnotherReallyLongString", "YetAnotherOne", "ThisIsGettingPrettyLongNow"} contains 1 item(s) less. all one one line, which is really hard to read
When you've got a DTO or something with a ToString that can be 100 characters long, and a collection of 15 of those DTOs, it quickly becomes unusable
I've had a look at the source and I've seen that AssertionScope has a UsingLineBreaks option, but my question is, how do I actually access the scope so that I can set that option from a normal test like the one above?
James Webster
@jimmcslim
Trying to compare two lists of dictionaries ( IDictionary<string, object> ), but want to ignore missing keys on (i.e. one dictionary should be a superset of the other, any keys that match should have the same value)
gengxian86
@gengxian86
Hi Guys, I am trying to install Fluent Assertions for Xamarin 4.2.2
but to no avail
Dennis Doomen
@dennisdoomen

Trying to compare two lists of dictionaries ( IDictionary<string, object> ), but want to ignore missing keys on (i.e. one dictionary should be a superset of the other, any keys that match should have the same value)

Nope. There's no API for that yet.

Hi Guys, I am trying to install Fluent Assertions for Xamarin 4.2.2
but to no avail

Did you try https://www.nuget.org/packages/FluentAssertions.Xamarin ?

gengxian86
@gengxian86
@dennisdoomen yup, i tried that but it keep complaining that is couldnt find the required package. are there any dependency for it? im using .net 4.61
Adam Rodger
@adamrodger
@dennisdoomen do you have any ideas on how I can set the UsingLineBreaks option for a given assertion when comparing collection (as in my example above)?
Dennis Doomen
@dennisdoomen
@adamrodger You can't. It is something that is used within the assertion classes
Stuart Blackler
@Im5tu
I've got a bit of free time coming up, thought i'd start to contribute some stuff back. Anything that you want taking a look at as a higher priority?
Dennis Doomen
@dennisdoomen
@Im5tu That would be awesome. There's a lot of stuff up for grabs. I'm still reorganizing the develop branch for v5, but I'll just forward port anything that lands on master
Stuart Blackler
@Im5tu
Do you need a hand with the reorg?
Dennis Doomen
@dennisdoomen
Sure. I'm trying to get my PR to compile properly. Have to go through all the #ifdefs and stuff to make it work. But I've overcommitted a bit too much. See fluentassertions/fluentassertions#558