Thursday, January 14, 2010

NHibernate – Writing unit tests using VSTS (Part 7)


Before writing the first test case against NHibernate, you should make sure that all the required files that are used for NHibernate are available for the test method. For accessing SQL Server 2005 express using NHibernate you should copy the sqlServer dll’s from the Microsoft SQL Server Compact Edition directory in Program files folder to the Lib folder where the NHibernate assemblies are deployed.


Now you need to configure the test environment settings. In the deployment settings of test configuration, make sure that you have mentioned the BuildFolder and the assemblies from the shared location.


Once the deployment options are configured we are good to write our first test case.
[TestClass]
public class NHibernateConfiguration_Fixture
{
    //VSTS framework in 2008 fails to read the NHibernate's configuration on load.
    //To set path for looking the configuration file to the current execution path, you need to manually specify the location.
    //Once the location is set, the RelativeSearchPath property is cleared by calling the ClearPrivatePath() method.

    [AssemblyInitialize]
    public static void AssemblyInitialize(TestContext context)
    {
        AppDomain.CurrentDomain.SetData("APPBASE", System.Environment.CurrentDirectory);
        AppDomain.CurrentDomain.ClearPrivatePath();
    }

    [TestMethod]
    public void can_open_configuration_info_and_create_session()
    {
        var __session = SessionManager.CurrentInstance.Session;
        Assert.IsNotNull(__session);
    }
}
The AssemblyInitilize method set’s the current execution path to look for the configuration file. Once the location is set, we need to clear the Relative search path by making a call to ClearPrivatePath method.
If your test fails double check your test deployment folder and see whether all the assemblies are copied. Next we’ll create our repositories and execute CRUD operations on Customer table.

No comments: