Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 684375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:48:35+00:00 2026-05-14T01:48:35+00:00

I would like to unit test a DEPENDENT service layer which allows me to

  • 0

I would like to unit test a DEPENDENT service layer which allows me to perform CRUD operation without mocking using NUnit. I know this is probably bad practice but I want to give it a try anyway – even if the tests have to run over night. My data is persisted using NHibernate and I have implemented a little library that ‘bootstraps’ the database which I could use in a [Setup] method. I am just wondering if someone has done something similar and what the fastest method for bootstrapping the database is. I am using something like this:

var cfg = new Configuration();
            cfg.Configure();
            cfg.AddAssembly("Bla");
            new SchemaExport(cfg).Execute(false, true, false);

to establish the db schema. After that I populate some lookup tables from some Excel tables.

Any feedback would be very much appreciated. Thanks.

Christian

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T01:48:36+00:00Added an answer on May 14, 2026 at 1:48 am

    Ayende has a good example of this approach in this blog post. His example is shown below with my comments.

    As the Configuration is expensive to create, it is created only once per test run. A SQLite in-memory database is used as this is the fastest way to do queries.

    public class InMemoryDatabaseTest : IDisposable
    {
        private static Configuration Configuration;
        private static ISessionFactory SessionFactory;
        protected ISession session;
    
        public InMemoryDatabaseTest(Assembly assemblyContainingMapping)
        {
            if (Configuration == null)
            {
                Configuration = new Configuration()
                    .SetProperty(Environment.ReleaseConnections,"on_close")
                    .SetProperty(Environment.Dialect, typeof (SQLiteDialect).AssemblyQualifiedName)
                    .SetProperty(Environment.ConnectionDriver, typeof(SQLite20Driver).AssemblyQualifiedName)
                    .SetProperty(Environment.ConnectionString, "data source=:memory:")
                    .SetProperty(Environment.ProxyFactoryFactoryClass, typeof (ProxyFactoryFactory).AssemblyQualifiedName)
                    .AddAssembly(assemblyContainingMapping);
    
                SessionFactory = Configuration.BuildSessionFactory();
            }
    
            session = SessionFactory.OpenSession();
    
            new SchemaExport(Configuration).Execute(true, true, false, true, session.Connection, Console.Out);
        }
    
        public void Dispose()
        {
            session.Dispose();
        }
    }
    

    When using this, each test starts by creating required data.

    public class BlogTestFixture : InMemoryDatabaseTest
    {
        public BlogTestFixture() : base(typeof(Blog).Assembly)
        {
        }
    
        [Fact]
        public void CanSaveAndLoadBlog()
        {
            object id;
    
            using (var tx = session.BeginTransaction())
            {
                id = session.Save(new Blog
                {
                    AllowsComments = true,
                    CreatedAt = new DateTime(2000,1,1),
                    Subtitle = "Hello",
                    Title = "World",
                });
    
                tx.Commit();
            }
    
            session.Clear();
    
    
            using (var tx = session.BeginTransaction())
            {
                var blog = session.Get<Blog>(id);
    
                Assert.Equal(new DateTime(2000, 1, 1), blog.CreatedAt);
                Assert.Equal("Hello", blog.Subtitle);
                Assert.Equal("World", blog.Title);
                Assert.True(blog.AllowsComments);
    
                tx.Commit();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My main JavaScript framework is jQuery , so I would like my unit test
I would like to know what a Unit test and Web Test is in
I would like to be able to add a message to a unit test,
I'm writing a unit test with Boost.Unit, and I would like to include basic
I would like to know what would be the best way to do unit
Would like to get a list of advantages and disadvantages of using Stored Procedures.
I would like to test a string containing a path to a file for
I would like to sort an array in ascending order using C/C++ . The
I would like to have a reference for the pros and cons of using
I am using SQL Express databases as part of a unit test project in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.