NHibernate.PersistenceTesting 1.4.1

dotnet add package NHibernate.PersistenceTesting --version 1.4.1
                    
NuGet\Install-Package NHibernate.PersistenceTesting -Version 1.4.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="NHibernate.PersistenceTesting" Version="1.4.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="NHibernate.PersistenceTesting" Version="1.4.1" />
                    
Directory.Packages.props
<PackageReference Include="NHibernate.PersistenceTesting" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add NHibernate.PersistenceTesting --version 1.4.1
                    
#r "nuget: NHibernate.PersistenceTesting, 1.4.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.
#:package NHibernate.PersistenceTesting@1.4.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=NHibernate.PersistenceTesting&version=1.4.1
                    
Install as a Cake Addin
#tool nuget:?package=NHibernate.PersistenceTesting&version=1.4.1
                    
Install as a Cake Tool

NHibernate.PersistenceTesting

Build status NuGet

Code based on the testing framework in Fluent NHibernate and adopted to work with vanilla NHibernate. Documentation adapted from Fluent NHibernate wiki.

Downloads

The latest release of NHibernate.PersistenceTesting is available on NuGet.

Usage

You can quickly test your mappings using the PersistenceSpecification<T> class. Let's start with a simple Employee mapping:

public class Employee
{
    public virtual int Id { get; private set; }
    public virtual string FirstName { get; set; }
    public virtual string LastName { get; set; }
}

public class EmployeeMap : ClassMapping<Employee>
{
    public EmployeeMap()
    {
        Id(x => x.Id);
        Property(x => x.FirstName);
        Property(x => x.LastName);
    }
}

To test this mapping:

using NHibernate.PersistenceTesting;

[Test]
public void CanCorrectlyMapEmployee()
{
    new PersistenceSpecification<Employee>(session)
        .CheckProperty(c => c.Id, 1)
        .CheckProperty(c => c.FirstName, "John")
        .CheckProperty(c => c.LastName, "Doe")
        .VerifyTheMappings();
}

This test will:

  • create an Employee instance
  • insert the Employee into the database
  • retrieve the record into a new Employee instance
  • verify the retrieved Employee matches the original

Testing references

Let's consider an Employee mapping that includes a reference to a Store:

public class EmployeeMap : ClassMapping<Employee>
{
    public EmployeeMap()
    {
        Id(x => x.Id);
        Property(x => x.FirstName);
        Property(x => x.LastName);
        ManyToOne(x => x.Store);
    }
}

When verifying mappings, the Object.Equals() method is used to compare the retrieved values to the inserted values. This works as expected for primitive types and types that override Object.Equals(). For all other types, such as our Store class, the default implementation of Object.Equals() checks that both values point to the same instance of the object. We will always have different instances because the test flushes and clears the session cache after inserting the record. Instead, we want to verify the correct Store was loaded by comparing the Store's Id (and possibly other properties).

The PersistenceSpecification constructor allows us to pass in an IEqualityComparer to customize how each value is compared.

[Test]
public void CanCorrectlyMapEmployee()
{
    new PersistenceSpecification<Employee>(session, new CustomEqualityComparer())
        .CheckProperty(c => c.Id, 1)
        .CheckProperty(c => c.FirstName, "John")
        .CheckProperty(c => c.LastName, "Doe")
        .CheckReference(c => c.Store, new Store() {Name = "MyStore"})
        .VerifyTheMappings();
}

public class CustomEqualityComparer: IEqualityComparer
{
    public bool Equals(object x, object y)
    {
        if (x == null || y == null)
        {
            return false;
        }
        if (x is Store && y is Store)
        {
            return ((Store) x).Id == ((Store) y).Id;
        }
        return x.Equals(y);
    }

    public int GetHashCode(object obj)
    {
        throw new NotImplementedException();
    }
}

CheckReference automatically saves the referenced entity before saving the entity being tested. The transient Store in this example therefore does not require cascading on the Employee.Store mapping. The Store entity must have its own mapping, and its required properties and references must be valid for saving.

Testing lists

CheckList saves each element individually while CheckComponentList relies on cascading or the user saving.

Contributors

For list of contributors, see Fluent NHibernate.

NHibernate.PersistenceTesting is © 2017-2026 Nathan Brown and contributors and © 2008-2015 James Gregory and contributors under the BSD license

Fluent NHibernate is © 2008-2015 James Gregory and contributors under the BSD license

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  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 is compatible.  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.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.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 is compatible.  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.4.1 136 10/28/2026
1.4.0 137 10/28/2026
1.3.0 1,006 6/8/2020
1.2.0.15 743 6/7/2020
1.1.0.4 1,563 8/9/2017