From 4b0f15496e2fdfa3c6e65ad1617f09fa0e15af4e Mon Sep 17 00:00:00 2001 From: Gunnar Liljas Date: Sat, 10 Oct 2020 21:00:16 +0200 Subject: [PATCH 1/2] Temporary fix to allow necessary mocking of ISession --- .../Testing/PersistenceSpecificationTester.cs | 2 +- ...PersistenceSpecificationTransactionTest.cs | 18 ++++++++++++++--- .../Testing/PersistenceSpecification.cs | 20 ++++++++++++++++--- 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/src/FluentNHibernate.Testing/Testing/PersistenceSpecificationTester.cs b/src/FluentNHibernate.Testing/Testing/PersistenceSpecificationTester.cs index 91b9b20de..e8e6ece9c 100644 --- a/src/FluentNHibernate.Testing/Testing/PersistenceSpecificationTester.cs +++ b/src/FluentNHibernate.Testing/Testing/PersistenceSpecificationTester.cs @@ -201,7 +201,7 @@ public void Setup() var transaction = A.Fake(); var session = A.Fake(); A.CallTo(() => session.BeginTransaction()).Returns(transaction); - + A.CallTo(() => session.Transaction).Returns(transaction); sessionSource = A.Fake(); A.CallTo(() => sessionSource.CreateSession()).Returns(session); } diff --git a/src/FluentNHibernate.Testing/Testing/PersistenceSpecificationTransactionTest.cs b/src/FluentNHibernate.Testing/Testing/PersistenceSpecificationTransactionTest.cs index 62d649d55..fe59eb33c 100644 --- a/src/FluentNHibernate.Testing/Testing/PersistenceSpecificationTransactionTest.cs +++ b/src/FluentNHibernate.Testing/Testing/PersistenceSpecificationTransactionTest.cs @@ -29,13 +29,17 @@ public void Should_Not_Open_A_New_Transaction_If_One_Is_Passed() A.CallTo(() => transaction.IsActive).Returns(true); var session = A.Fake(); +#pragma warning disable CS0618 // Type or member is obsolete A.CallTo(() => session.Transaction).Returns(transaction); +#pragma warning restore CS0618 // Type or member is obsolete var spec = new PersistenceSpecification(session); spec.VerifyTheMappings(); A.CallTo(() => session.BeginTransaction()).MustNotHaveHappened(); - A.CallTo(() => session.Transaction).MustHaveHappened(Repeated.Exactly.Twice); +#pragma warning disable CS0618 // Type or member is obsolete + A.CallTo(() => session.Transaction).MustHaveHappened(Repeated.Exactly.Once); +#pragma warning restore CS0618 // Type or member is obsolete } [Test] @@ -46,13 +50,17 @@ public void Should_Open_A_New_Transaction_If_None_Is_Passed() A.CallTo(() => transaction.IsActive).Returns(false); var session = A.Fake(); +#pragma warning disable CS0618 // Type or member is obsolete A.CallTo(() => session.Transaction).Returns(transaction); +#pragma warning restore CS0618 // Type or member is obsolete A.CallTo(() => session.BeginTransaction()).Returns(transaction); var spec = new PersistenceSpecification(session); spec.VerifyTheMappings(); - A.CallTo(() => session.Transaction).MustHaveHappened(Repeated.Exactly.Twice); +#pragma warning disable CS0618 // Type or member is obsolete + A.CallTo(() => session.Transaction).MustHaveHappened(Repeated.Exactly.Once); +#pragma warning restore CS0618 // Type or member is obsolete } [Test] @@ -62,7 +70,9 @@ public void Passed_Transaction_Should_Apply_For_Reference_Saving() A.CallTo(() => transaction.IsActive).Returns(true); var session = A.Fake(); +#pragma warning disable CS0618 // Type or member is obsolete A.CallTo(() => session.Transaction).Returns(transaction); +#pragma warning restore CS0618 // Type or member is obsolete A.CallTo(() => session.Get(null)).WithAnyArguments() .Returns( new Cat @@ -81,7 +91,9 @@ public void Passed_Transaction_Should_Apply_For_Reference_Saving() spec.VerifyTheMappings(); - A.CallTo(() => session.Transaction).MustHaveHappened(Repeated.Exactly.Twice); +#pragma warning disable CS0618 // Type or member is obsolete + A.CallTo(() => session.Transaction).MustHaveHappened(Repeated.Exactly.Once); +#pragma warning restore CS0618 // Type or member is obsolete A.CallTo(() => session.BeginTransaction()).MustNotHaveHappened(); } diff --git a/src/FluentNHibernate/Testing/PersistenceSpecification.cs b/src/FluentNHibernate/Testing/PersistenceSpecification.cs index fb416c54a..fdcad6f50 100644 --- a/src/FluentNHibernate/Testing/PersistenceSpecification.cs +++ b/src/FluentNHibernate/Testing/PersistenceSpecification.cs @@ -1,8 +1,10 @@ -using System.Collections; +using System; +using System.Collections; using System.Collections.Generic; using FluentNHibernate.Testing.Values; using FluentNHibernate.Utils; using NHibernate; +using NHibernate.Impl; namespace FluentNHibernate.Testing { @@ -31,10 +33,22 @@ public PersistenceSpecification(ISession session) public PersistenceSpecification(ISession session, IEqualityComparer entityEqualityComparer) { currentSession = session; - hasExistingTransaction = currentSession.GetCurrentTransaction() != null || System.Transactions.Transaction.Current != null; + hasExistingTransaction = HasExistingTransaction(currentSession) || System.Transactions.Transaction.Current != null; this.entityEqualityComparer = entityEqualityComparer; } + //Only to make fake sessions possible in testing + private bool HasExistingTransaction(ISession currentSession) + { + if (currentSession is SessionImpl) + { + return currentSession.GetCurrentTransaction() != null; + } +#pragma warning disable CS0618 // Type or member is obsolete + return currentSession.Transaction?.IsActive == true; +#pragma warning restore CS0618 // Type or member is obsolete + } + public T VerifyTheMappings() { return VerifyTheMappings(typeof(T).InstantiateUsingParameterlessConstructor()); @@ -63,7 +77,7 @@ public T VerifyTheMappings(T first) // It's a bit naive right now because it fails on the first failure allProperties.ForEach(p => p.CheckValue(second)); - return second; + return second; } public void TransactionalSave(object propertyValue) From 7c1bcdf0d41b4013b9074b3eb5aaae6369d3618f Mon Sep 17 00:00:00 2001 From: Gunnar Liljas Date: Sat, 10 Oct 2020 21:12:04 +0200 Subject: [PATCH 2/2] Nuget update --- .../Examples.FirstAutomappedProject.csproj | 2 +- .../Examples.FirstProject.csproj | 2 +- .../FluentNHibernate.Specs.csproj | 12 ++++++------ .../ConventionFinderTests/AddingTypeTests.cs | 10 +++++----- .../FluentNHibernate.Testing.csproj | 15 ++++++++------- .../Infrastructure/ContainerTester.cs | 2 +- .../PersistenceSpecificationTransactionTest.cs | 6 +++--- .../Testing/Values/ListSpecs.cs | 4 ++-- 8 files changed, 27 insertions(+), 26 deletions(-) diff --git a/src/Examples.FirstAutomappedProject/Examples.FirstAutomappedProject.csproj b/src/Examples.FirstAutomappedProject/Examples.FirstAutomappedProject.csproj index f4d7c73e6..bd3b1615e 100644 --- a/src/Examples.FirstAutomappedProject/Examples.FirstAutomappedProject.csproj +++ b/src/Examples.FirstAutomappedProject/Examples.FirstAutomappedProject.csproj @@ -14,7 +14,7 @@ - + diff --git a/src/Examples.FirstProject/Examples.FirstProject.csproj b/src/Examples.FirstProject/Examples.FirstProject.csproj index a3268bdbb..94912ae4f 100644 --- a/src/Examples.FirstProject/Examples.FirstProject.csproj +++ b/src/Examples.FirstProject/Examples.FirstProject.csproj @@ -14,7 +14,7 @@ - + diff --git a/src/FluentNHibernate.Specs/FluentNHibernate.Specs.csproj b/src/FluentNHibernate.Specs/FluentNHibernate.Specs.csproj index 79ff517e9..e856a6eff 100644 --- a/src/FluentNHibernate.Specs/FluentNHibernate.Specs.csproj +++ b/src/FluentNHibernate.Specs/FluentNHibernate.Specs.csproj @@ -12,12 +12,12 @@ - - - - - - + + + + + + diff --git a/src/FluentNHibernate.Testing/ConventionFinderTests/AddingTypeTests.cs b/src/FluentNHibernate.Testing/ConventionFinderTests/AddingTypeTests.cs index aa8bc0f2e..0abf83c86 100644 --- a/src/FluentNHibernate.Testing/ConventionFinderTests/AddingTypeTests.cs +++ b/src/FluentNHibernate.Testing/ConventionFinderTests/AddingTypeTests.cs @@ -22,7 +22,7 @@ public void AddingSingleShouldntThrowIfHasParameterlessConstructor() { Action act = () => finder.Add(); - act.ShouldNotThrow(); + act.Should().NotThrow(); } [Test] @@ -30,7 +30,7 @@ public void AddingSingleShouldntThrowIfHasIConventionFinderConstructor() { Action act = () => finder.Add(); - act.ShouldNotThrow(); + act.Should().NotThrow(); } @@ -39,7 +39,7 @@ public void AddingSingleShouldThrowIfNoParameterlessConstructor() { Action act = () => finder.Add(); - act.ShouldThrow(); + act.Should().Throw(); } [Test] @@ -47,7 +47,7 @@ public void AddingSingleShouldThrowIfNoIConventionFinderConstructor() { Action act = () => finder.Add(); - act.ShouldThrow(); + act.Should().Throw(); } [Test] @@ -55,7 +55,7 @@ public void AddingAssemblyShouldntThrowIfNoIConventionFinderConstructor() { Action act = () => finder.AddAssembly(typeof(ConventionWithoutValidConstructor).Assembly); - act.ShouldNotThrow(); + act.Should().NotThrow(); } } diff --git a/src/FluentNHibernate.Testing/FluentNHibernate.Testing.csproj b/src/FluentNHibernate.Testing/FluentNHibernate.Testing.csproj index f92b18f39..ef04bf29a 100644 --- a/src/FluentNHibernate.Testing/FluentNHibernate.Testing.csproj +++ b/src/FluentNHibernate.Testing/FluentNHibernate.Testing.csproj @@ -20,13 +20,14 @@ - - - - - - - + + + + + + + + diff --git a/src/FluentNHibernate.Testing/Infrastructure/ContainerTester.cs b/src/FluentNHibernate.Testing/Infrastructure/ContainerTester.cs index e16c6cf2a..3e893174f 100644 --- a/src/FluentNHibernate.Testing/Infrastructure/ContainerTester.cs +++ b/src/FluentNHibernate.Testing/Infrastructure/ContainerTester.cs @@ -31,7 +31,7 @@ public void ShouldThrowExceptionWhenResolvingUnregisteredType() { Action act = () => container.Resolve(); - act.ShouldThrow() + act.Should().Throw() .WithMessage("Unable to resolve dependency: '" + typeof(IExample).FullName + "'"); } diff --git a/src/FluentNHibernate.Testing/Testing/PersistenceSpecificationTransactionTest.cs b/src/FluentNHibernate.Testing/Testing/PersistenceSpecificationTransactionTest.cs index fe59eb33c..89712bd5e 100644 --- a/src/FluentNHibernate.Testing/Testing/PersistenceSpecificationTransactionTest.cs +++ b/src/FluentNHibernate.Testing/Testing/PersistenceSpecificationTransactionTest.cs @@ -38,7 +38,7 @@ public void Should_Not_Open_A_New_Transaction_If_One_Is_Passed() A.CallTo(() => session.BeginTransaction()).MustNotHaveHappened(); #pragma warning disable CS0618 // Type or member is obsolete - A.CallTo(() => session.Transaction).MustHaveHappened(Repeated.Exactly.Once); + A.CallTo(() => session.Transaction).MustHaveHappened(1, Times.Exactly); #pragma warning restore CS0618 // Type or member is obsolete } @@ -59,7 +59,7 @@ public void Should_Open_A_New_Transaction_If_None_Is_Passed() spec.VerifyTheMappings(); #pragma warning disable CS0618 // Type or member is obsolete - A.CallTo(() => session.Transaction).MustHaveHappened(Repeated.Exactly.Once); + A.CallTo(() => session.Transaction).MustHaveHappened(1, Times.Exactly); #pragma warning restore CS0618 // Type or member is obsolete } @@ -92,7 +92,7 @@ public void Passed_Transaction_Should_Apply_For_Reference_Saving() spec.VerifyTheMappings(); #pragma warning disable CS0618 // Type or member is obsolete - A.CallTo(() => session.Transaction).MustHaveHappened(Repeated.Exactly.Once); + A.CallTo(() => session.Transaction).MustHaveHappened(1, Times.Exactly); #pragma warning restore CS0618 // Type or member is obsolete A.CallTo(() => session.BeginTransaction()).MustNotHaveHappened(); } diff --git a/src/FluentNHibernate.Testing/Testing/Values/ListSpecs.cs b/src/FluentNHibernate.Testing/Testing/Values/ListSpecs.cs index d1c386e6c..860aa0560 100644 --- a/src/FluentNHibernate.Testing/Testing/Values/ListSpecs.cs +++ b/src/FluentNHibernate.Testing/Testing/Values/ListSpecs.cs @@ -417,7 +417,7 @@ public override void because() public void should_perform_the_check_with_the_custom_equality_comparer() { A.CallTo(() => sut.EntityEqualityComparer.Equals(null, null)) - .WithAnyArguments().MustHaveHappened(Repeated.Exactly.Times(3)); + .WithAnyArguments().MustHaveHappened(3, Times.Exactly); } } @@ -443,7 +443,7 @@ public void should_perform_the_check_with_the_custom_equality_comparer() { A.CallTo(() => sut.EntityEqualityComparer.Equals(null, null)) .WithAnyArguments() - .MustHaveHappened(Repeated.Exactly.Once); + .MustHaveHappened(1, Times.Exactly); } }