locked
Unit Test my data access layer,having problem with database connection RRS feed

  • Question

  • I am developing a multi-tier web application, with Web, business and data tiers.  My tests are written to go against the business tier, in turn flows into data access tier.    I am using Enterprise Library 5.0 in the data access tier to simplify data access operations.  I am using this line to get the "database" object from enterprise library:

    protected Database _dbTitleV = DatabaseFactory.CreateDatabase("TitleVDatabase");

    the line above works when I run the web application, the connection string is in the web.config file.  However, when I run the test from the test project, it fails, because at that context, there is no config file since I run tests against the business tier, which is a class library.   I would think this is a very basic problem, somehow I was able to figure out.  DatabaseFactory.CreateDatabase seems to be hard wired to look for connection strings in config file, which I don't have in the test context.  Thanks in advance.

     

    Tools Used:  visual Studio 2010, Enterprise Library 5.0.  When I create the test project, I add new project and select "Test Projects" --> "Test Documents" from visual studio

     

    Below is my data access layer class file

    =======

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using System.Data.SqlClient;

    usingSystem.Data;

     using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;

    using Microsoft.Practices.EnterpriseLibrary.Data;

    using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

     

    namespace TitleVReporting.Data

    {

        public class User

        {

           

            protected Database _dbTitleV = DatabaseFactory.CreateDatabase("TitleVDatabase");

            protected Database _dbSharedServices = DatabaseFactory.CreateDatabase("LyoSharedServicesDatabase");

     

           

            

           

            public User()

            {

                //try

                //{

                //    _dbTitleV = DatabaseFactory.CreateDatabase("TitleVDatabase");

                //    _dbSharedServices = DatabaseFactory.CreateDatabase("LyoSharedServicesDatabase");

                //}

                //catch(Exception ex)

                //{

                //    _dbTitleV = DatabaseFactory.CreateDatabase(

                //}

     

            }

     

            public DataSet ValidateUser(string userid)

            {

              

                DataSet ds = _dbTitleV.ExecuteDataSet("usp_GetUserInfo", userid);

                return ds;

            }

     

            public DataSet GetUserInfo(string userid)

            {

                //return Server.UrlEncode("jerry.li@lyondellbasell.com");           

                DataSet ds = _dbSharedServices.ExecuteDataSet("usp_Person_Master_Select_By_User_Id", userid);

                return ds;

              

     

            }

     

        }

    }

     



    Tuesday, April 26, 2011 8:28 PM

Answers