Note that there are some explanatory texts on larger screens.

plurals
  1. PONHibernate.PropertyValueException : not-null property references a null or transient and dependent unit tests
    text
    copied!<p>I have a strange behaviour when one unit test becomes dependent on another one and fails because of object in NHiberante session. </p> <p>I'm getting the 'NHibernate.PropertyValueException : not-null property references a null or transient' only if I run all unit tests from fixture (for the simplicity I have only two tests). If I run one of them it always passed. </p> <p>I feel that I should do some clean-up. I tried session.Clean() and session.Evict(obj), but it didn't help. Could somebody explain what is going on here?</p> <p>Entity:</p> <pre><code>public class Order { public virtual Guid Id { get; protected set; } public virtual string Name { get; set; } } </code></pre> <p>Mapping (with Loquacious API):</p> <pre><code>public class OrderMapping : ClassMapping&lt;Order&gt; { public OrderMapping() { Id(e =&gt; e.Id, m =&gt; { m.Generator(Generators.Guid); m.Column("OrderId"); }); Property(e =&gt; e.Name, m =&gt; m.NotNullable(true)); } } </code></pre> <p>Fixture ctor (the in-memory database are used):</p> <pre><code>var config = new Configuration(); config.CurrentSessionContext&lt;ThreadStaticSessionContext&gt;(); config.DataBaseIntegration(db =&gt; { db.ConnectionString = "uri=file://:memory:,Version=3"; db.Dialect&lt;SQLiteDialect&gt;(); db.Driver&lt;CsharpSqliteDriver&gt;(); db.ConnectionReleaseMode = ConnectionReleaseMode.OnClose; db.KeywordsAutoImport = Hbm2DDLKeyWords.AutoQuote; db.LogSqlInConsole = true; }) .SessionFactory() .GenerateStatistics(); var mapper = new ModelMapper(); mapper.AddMappings(Assembly.GetExecutingAssembly().GetExportedTypes()); config.AddMapping(mapper.CompileMappingForAllExplicitlyAddedEntities()); ISessionFactory sessionFactory = config.BuildSessionFactory(); this.session = sessionFactory.OpenSession(); // This will leave the connection open new SchemaExport(config).Execute( true, true, false, this.session.Connection, null); CurrentSessionContext.Bind(this.session); </code></pre> <p>Unit tests:</p> <pre><code>[Test] [ExpectedException(typeof(PropertyValueException))] public void Order_name_is_required() { var order = new Order(); this.session.Save(order); } [Test] public void Order_was_updated() { var order = new Order { Name = "Name 1" }; this.session.Save(order); this.session.Flush(); order.Name = "Name 2"; this.session.Update(order); Assert.AreEqual(this.session.Get&lt;Order&gt;(order.Id).Name, "Name 2"); } </code></pre> <p>Order was updated fails with the 'NHibernate.PropertyValueException : not-null property references a null or transient' exception. And actually any other unit test will fail if write after.</p> <p><strong>EDIT 1</strong> Found a solution. Last time when I tried to clean-up the session I used</p> <pre><code>[TestFixtureTearDown] </code></pre> <p>instead of </p> <pre><code>[TearDown] public void TearDown() { this.session.Clear(); } </code></pre> <p>Which did all clean-up properly before <strong>each</strong> test run and allowed to use the same session and do not re-create the in-memory database structure. Sorry, I made the obvious mistake.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload