BeEquivalentTo does not exclude internal members
See original GitHub issueBeEquivalentTo
fails if an internal
property has a different value.
The documentation, section “Including properties and/or fields” states:
Barring other configuration, Fluent Assertions will include all public properties and fields
However, it also seems to take into account all internal
properties and fields.
Who is wrong? The documentation or the implementation? What is BeEquivalentTo
supposed to do?
In the following code sample, PrivateMembersShouldNotBeCompared succeeds but InternalMembersShouldNotBeCompared fails on the compare of property InternalProperty
.
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace UnitTestProject
{
[TestClass]
public class TestBeEquivalentTo
{
class TestSubject
{
private string PrivateProperty { get; }
internal string InternalProperty { get; }
public string PublicProperty { get; }
public TestSubject(string privateString, string internalString, string publicString)
{
PrivateProperty = privateString;
InternalProperty = internalString;
PublicProperty = publicString;
}
}
[TestMethod]
public void PrivateMembersShouldNotBeCompared()
{
TestSubject first = new TestSubject("A", "A", "A");
TestSubject second = new TestSubject("B", "A", "A");
first.Should().BeEquivalentTo(second);
}
[TestMethod]
public void InternalMembersShouldNotBeCompared()
{
TestSubject first = new TestSubject("A", "A", "A");
TestSubject second = new TestSubject("B", "B", "A");
first.Should().BeEquivalentTo(second);
}
}
}
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:9 (5 by maintainers)
Top Results From Across the Web
FluentAssertions won't exclude missing members on object ...
I'm trying to exclude these extra properties with the ExcludingMissingMembers options and even explicitly by excluding each member by itself ...
Read more >Object graph comparison
If you want to exclude certain (potentially deeply nested) individual members using the Excluding() method: orderDto.Should().BeEquivalentTo(order, ...
Read more >Comparing objects with disparate members in Fluent Assertions
Imagine you want to assert that an OrderDto is equivalent to an Order entity using BeEquivalentTo. This is a pretty common thing to...
Read more >Fluent Assertions 10 Top Tips
BeEquivalentTo() is very powerful and has plenty of options. Among its features, you can select properties and/or fields to include or exclude, ...
Read more >Fluent Assertions: Fluently Assert the Result of .NET Tests
Selecting members. If you want, you can exclude some members using the Excluding option: personDto.Should ...
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
I found a work-around that only checks public properties:
Time 😉