How to compare two Json objects using C#

I did a bit more digging and was able to find out why the OP’s test code doesn’t run as expected. I was able to fix it by installing and using the FluentAssertions.Json nuget package. One important thing: Be sure to include using FluentAssertions.Json otherwise false positives may occur. Test code is the following: using … Read more

How to assert all items in a collection using fluent-assertions?

The recommended way is to use OnlyContain: items.Should().OnlyContain(x => x.IsActive, “because I said so!”); These will also work: items.All(x => x.IsActive).Should().BeTrue(“because I said so!”); items.Select(x => x.IsActive.Should().BeTrue(“because I said so!”)) .All(x => true); Note that the last line (.All(x => true)) forces the previous Select to execute for each item.

FluentAssertions Asserting multiple properties of a single object

The .Match() solution does not return a good error message. So if you want to have a good error and only one assert then use: result.Should().BeEquivalentTo(new MyResponseObject() { Property1 = “something”, Property2 = “anotherthing” }); Anonymous objects (use with care!) If you want to only check certain members then use: result.Should().BeEquivalentTo(new { Property1 = “something”, … Read more

FluentAssertions: equivalence of sorted lists

By default, ShouldBeEquivalentTo() will ignore the order in the collections because in most cases two collections are equivalent if they contain the same items in any order. If you do care about the order, just use one of the overloads of WithStrictOrdering() on the options => parameter. Example: var myList = Enumerable.Range(1, 5); var expected … Read more

FluentAssertions: ShouldBeEquivalentTo vs Should().Be() vs Should().BeEquivalentTo()?

I agree this is confusing. Should().BeEquivalentTo() should actually be called Should().EqualInAnyOrder() or something like that. As you said, it uses the Equals implementation of the involved objects to see if all of the ones in the expected collection appear in the actual collection, regardless of order. I’ll need to fix that for the next major … Read more

How to use Fluent Assertions to test for exception in inequality tests?

You may try this approach. [Test] public void GreaterThan_NullAsRhs_ThrowsException() { var lhs = new ClassWithOverriddenOperator(); var rhs = (ClassWithOverriddenOperator) null; Action comparison = () => { var res = lhs > rhs; }; comparison.Should().Throw<Exception>(); } It doesn’t look neat enough. But it works. Or in two lines Func<bool> compare = () => lhs > rhs; … Read more

How to use Exclude in FluentAssertions for property in collection?

[*] What about? expected.ShouldBeEquivalentTo(actualA, options => options.Excluding(su => (su.RuntimeType == typeof(ClassB)) && (su.PropertyPath.EndsWith(“Id”)));` Or you could do a RegEx match on the property path, such as expected.ShouldBeEquivalentTo(actualA, options => options.Excluding(su => (Regex.IsMatch (“Children\[.+\]\.ID”)); I actually like that last one, but the regex stuff makes it a bit difficult to read. Maybe I should extend ISubjectInfo … Read more

Testing for exceptions in async methods

You should use Func<Task> instead of Action: [Test] public void TestFail() { Func<Task> f = async () => { await Fail(); }; f.ShouldThrow<Exception>(); } That will call the following extension which is used to verify asynchronous methods public static ExceptionAssertions<TException> ShouldThrow<TException>( this Func<Task> asyncAction, string because = “”, params object[] becauseArgs) where TException : Exception … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)