0

I am using Fluent NHibernate and trying to do unit testing. Now I have a base test class which looks as follows:

[TestClass]
public abstract class BaseTest<TEntity> 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 => m.FluentMappings.AddFromAssemblyOf<AdminTest>())
                                .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<TEntity>(entity.Id);

        Assert.IsNotNull(reloadedEntity);
        AssertAreEqual(entity, reloadedEntity);
        AssertValidId(reloadedEntity);
    }

There are also other methods which update and delete an entity. And I have AdminTest class which inherits BaseTest. In AdminTest I have following method:

[TestClass]
public class AdminTest : BaseTest<Admin>
{
    [TestMethod]
    public void SelectWorks()
    {
        IList<Admin> admins = session.QueryOver<Admin>().List();
        Assert.AreNotEqual(0, admins.Count);
    }
}

Here I always have exception, because session is null. 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

  1. runs test-methods from BaseTest (there it configures database and creates session)
  2. runs selectWorks method. Here I was thinking it should use session from BaseTest

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, .

1 Answers1

0

I would suggest using [TestInitialize] and [TestCleanup] in your abstract base class and doing something like the following:

    [TestInitialize]
    public void TestInitialize()
    {
        Console.Out.WriteLine("Get a ISession object");
    }

    [TestCleanup]
    public void TestCleanup()
    {
        Console.Out.WriteLine("Dispose ISession");
    }

Then in your child classes continue to do what you are doing:

    [TestMethod]
    public void DoDbWork()
    {
        Console.Out.WriteLine("Running a query via nhibernate");
    }

You really just want to ensure you have a clean session for each test. The attributes for TestInitialize and TestCleanup will run before and after each unit test. Here is the documentation for those attributes.

To ensure your ISession is in the right state,follow something like this.

Community
  • 1
  • 1
ElvisLives
  • 2,275
  • 2
  • 18
  • 24