Note that there are some explanatory texts on larger screens.

plurals
  1. PONhibernate unit testing. Child class cannot use Base class property
    primarykey
    data
    text
    <p>I am using Fluent NHibernate and trying to do unit testing. Now I have a base test class which looks as follows:</p> <pre><code>[TestClass] public abstract class BaseTest&lt;TEntity&gt; where TEntity : IBaseModel { private const string testDbFile = "test.db"; private static ISessionFactory sessionFactory; protected static ISession session; [TestMethod] public void configureDB() { try { if (sessionFactory == null) { sessionFactory = Fluently.Configure() .Database(SQLiteConfiguration.Standard .UsingFile(testDbFile)) .Mappings(m =&gt; m.FluentMappings.AddFromAssemblyOf&lt;AdminTest&gt;()) .ExposeConfiguration(BuildSchema) .BuildSessionFactory(); } } catch (Exception ex) { Console.WriteLine(ex.InnerException.Message); } } private static void BuildSchema(Configuration config) { new SchemaUpdate(config).Execute(false, true); } [TestMethod] public void sessionCreated() { session = sessionFactory.OpenSession(); } [TestMethod] public virtual void AddEntity_EntityWasAdded() { var entity = BuildEntity(); InsertEntity(entity); session.Evict(entity); var reloadedEntity = session.Get&lt;TEntity&gt;(entity.Id); Assert.IsNotNull(reloadedEntity); AssertAreEqual(entity, reloadedEntity); AssertValidId(reloadedEntity); } </code></pre> <p>There are also other methods which update and delete an entity. And I have <code>AdminTest</code> class which inherits <code>BaseTest</code>. In <code>AdminTest</code> I have following method:</p> <pre><code>[TestClass] public class AdminTest : BaseTest&lt;Admin&gt; { [TestMethod] public void SelectWorks() { IList&lt;Admin&gt; admins = session.QueryOver&lt;Admin&gt;().List(); Assert.AreNotEqual(0, admins.Count); } } </code></pre> <p>Here I always have exception, because session is <code>null</code>. Maybe I am wrong in the way of thinking how visual studio performs unit tests (I am newbie in it)? Now I think like that, visual studio works in the following way</p> <ol> <li>runs test-methods from <code>BaseTest</code> (there it configures database and creates session)</li> <li>runs <code>selectWorks</code> method. Here I was thinking it should use <code>session</code> from <code>BaseTest</code></li> </ol> <p>Could you explain what is wrong in my way of thinking? And I want to be able to query from child classes, what is the right way of doing it? Thanks, any help is appreciated, .</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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