FluentAssertions.Properties 1.0.1

dotnet add package FluentAssertions.Properties --version 1.0.1
NuGet\Install-Package FluentAssertions.Properties -Version 1.0.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="FluentAssertions.Properties" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add FluentAssertions.Properties --version 1.0.1
#r "nuget: FluentAssertions.Properties, 1.0.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install FluentAssertions.Properties as a Cake Addin
#addin nuget:?package=FluentAssertions.Properties&version=1.0.1

// Install FluentAssertions.Properties as a Cake Tool
#tool nuget:?package=FluentAssertions.Properties&version=1.0.1

FluentAssertions.Properties

Unofficial FluentAssertions extensions for testing the behavior of class/struct/record properties.

build workflow Coverage Status License nuget

Table of Contents

How?

Some common scenarios:

  • Testing that all properties from a class provide symmetric access, i.e. they return the same value that has been assigned to them
    var instanceUnderTest = new SampleDto();
    var testValues = new Fixture().Create<SampleDto>();

    instanceUnderTest
        .Properties()
        .ThatAreWritable
        .WhenCalledWithValuesFrom(testValues)
        .Should()
        .ProvideSymmetricAccess();

Speaking in the lingo of AutoFixture, we can say that ProvideSymmetricAccess() verifies that the properteis are "well-behaved writables" (see AutoFixture's WritablePropertyAssertion idiom).

  • Testing that getters/setters throw exceptions in certain cases
    var instanceUnderTest = new TestClass();
            
    instanceUnderTest
        .Properties()
        .ExactlyOfType<string>()
        .WhenCalledWith(string.Empty)
        .Should()
        .ThrowFromSetter<ArgumentException>()
        .WithMessage("Empty strings are not accepted.");
  • Selecting specific properties to test by their type and value
    var instanceUnderTest = new TestClass();
            
    instanceUnderTest
        .Properties()
        .ExactlyOfType<string>()
        .HavingValue("some value")
        .Should()
        .HaveCount(2);

or selecting individual properties by name

    var instanceUnderTest = new TestRecord();
    string testValue = Guid.NewGuid().ToString();

    instanceUnderTest
        .Properties(o => o.StringPropertyOne, o => o.StringPropertyTwo)
        .WhenCalledWith(testValue)
        .Should()
        .ProvideSymmetricAccess();

A more comprehensive explanation of the selection and assertions methods, provided by this library, can be found here and here.

Why?

Even if code is trivial you should still test it.

-- <cite>Mark Seemann</cite>

Why should I consider testing my class properties?

From the perspective of the caller, the public properties are part of the public "interface" of a type. They imply a contract - their semantics is such that one expects them to behave like public fields. However, they have accessor methods, which can contain logic that modifies the expected behavior. Implementing nontrivial logic in the accessors is sometimes considered to be an anti-pattern, and rightfully so - in order for a programmer to see how a particular property behaves, they have to open the implementation of the type and look inside the code. The presence of accessor methods is a big part of the reason why Microsoft has provided a list of bad practices and design guidelines, concerning the implementation of properties.

But that goes against the conventional wisdom!

There is a rule of thumb that says properties should not be tested if their getter and setter do not contain any logic, e.g. if they are auto-implemented. Even Robert C. Martin seems to think this way. However, there are other prominent authors, such as Mark Seeman, who strongly disagree. And there seems to be a not-so-small minority, which thinks that testing all public properties is necessary.

Installation

This library is distributed as a NuGet.

To install FluentAssertions.Properties, run the following command in the Package Manager Console:

PM> Install-Package FluentAssertions.Properties

Or use this command with the .NET CLI:

> dotnet add package FluentAssertions.Properties

Documentation

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.1 517 3/19/2022