0
namespace HRProject.Test
{
    using NUnit.Framework;
    using NHibernate;
    using HRProject.Model;
    using HRProject.Setup;
    using HRProject.Utils;
    using NHibernate.Linq;

    [TestFixture]
    public class RepositoryTest
    {
        [SetUp]
        public void SetUp()
        {
            ISession session = SessionHelper.GetSession();

            using (ITransaction tx = session.BeginTransaction())
            {
                foreach (Cashier c in session.Query<Cashier>().ToList<Cashier>())
                {
                    session.Delete(c);
                }

                tx.Commit();
            }

            using (ITransaction tx = session.BeginTransaction())
            {
                Cashier c1 = new Cashier(){Description = "D.Rathnayaka"};
                Cashier c2 = new Cashier(){Description = "K.Kumara"};
                Cashier c3 = new Cashier(){Description = "R.Gunapala"};

                session.Save(c1);
                session.Save(c2);
                session.Save(c3);

                tx.Commit();
            }
        }

        [Test]
        public void TestLoadUp() 
        { 
            ISession session = SessionHelper.GetSession();

            using (ITransaction tx = session.BeginTransaction())
            {
                Repository<Cashier> repo = new Repository<Cashier>();
                Assert.AreEqual(3, repo.Items.Count);
                Assert.AreEqual(1, (from Cashier c in repo.Items
                           where c.Description == "D.Rathnayaka"
                           select true).Count());

                tx.Commit();
            }
        }

        [Test]
        public void TestUpdate()
        {
            ISession session = SessionHelper.GetSession();

            using (ITransaction tx = session.BeginTransaction())
            {
                Repository<Cashier> repo = new Repository<Cashier>();
                Cashier cas = (from Cashier c in repo.Items
                            where c.Description == "D.Rathnayaka"
                            select c).Single();
                cas.Description = "K.R";
                session.Flush();
                tx.Commit();
            }



            using (ITransaction tx = session.BeginTransaction())
            {
                Repository<Cashier> repo = new Repository<Cashier>();
                //Fails here//
                Assert.IsNotNull((from Cashier c in repo.Items
                                  where c.Description == "K.R"
                                  select c).Single());
            }
        }
    }
}

Can someone tell me why this test fails? The DB does not get updated either. The repository class is below:

namespace HRProject.Utils
{
    using NHibernate;
    using HRProject.Setup;
    using NHibernate.Linq;

    class Repository<T> where T : class
    {
        public IList<T> Items {get; set;}

        public Repository()
        {
            Items = new List<T>();

            ISession session = SessionHelper.GetSession();
            using (ITransaction tx = session.BeginTransaction())
            {
                List<T> list = session.Query<T>().ToList();

                foreach (T obj in list)
                {
                    Items.Add((T)session.Merge(obj));
                }

                tx.Commit();
            }
        }

        public void AddRepositoryObject(T obj)
        {

        }
    }
}
3
  • What error does the test fail with? where does it fail? Commented Feb 20, 2012 at 6:09
  • HRProject.Test.RepositoryTest.TestUpdate: System.InvalidOperationException : Sequence contains no elements -- You can find it in this code by searching for '//Fails here//' Commented Feb 20, 2012 at 6:11
  • Have you tried commiting your transaction before your session? Commented Feb 23, 2012 at 21:15

1 Answer 1

1

I don't know the implementation details of your SessionHelper.GetSession method but I suspect that it opens new session on every call. If this is true then your test TestUpdate has two independent session objects. You open one session inside a test method and another session is opened by Repository. You update entity which is holded by the second session object but you commit transaction in the first session. You should have only one session object per interaction (session per conversation). Conside storing session in the CurrentSessionContext object.

Sign up to request clarification or add additional context in comments.

1 Comment

I don't create a new session everytime in SessionHelper.GetSession. I have a static ISession in it which remains open for the lifetime of the program.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.