junwei's profileRichard's footprint on ....PhotosBlogListsMore Tools Help

Blog


    September 18

    Unit Test is not easy

    I know that I should provide all necessary info for the function and i am able to do unit test comfortably. but under complex circumstance i don't know how, could you please shed light on it?

    My app contains three projects with in a solution,one is for web site, one is a class library project for business logic tier and the other is also class library project for data access tier.

    I paste my code here:

    Business Logic Tier:

    PO.cs

    public class PO

    {

     private PODao dao=new PODao();

     public bool InsertPO(POEntity)

     {

       ... //some validation logic

       return dao.InsertPO(POEntity);

     }

    }

     

    DataAccess Tier:

    PODao.cs

    public class PODao

    {

      public bool InsertPO(POEntity)

     {

       //use Enterprise Lib to execute database operation

       Database db = DatabaseFactory.CreateDatabase();

       ... //construct DbCommand

      try{

       db.ExecuteNonQuery(cmd);

       return true;

      }

      catch{

       //exception logic puts here

       return false;

      }

     }

    }

     

    Asp.net presentation tier:

    in the button click event handler on my POSubmit.aspx page,I wrote some codes like the followings:

    button_click()

    {

      POEntity entity=new POEntity();

      ....assign POEntity property value from web controls value

     PO po=new PO();

     po.InsertPO(entity);

    }

    and I put the connection string info in the web.config file.

    Now How can I do unit test for this InsertPO method of PO class in the business logic tier? My steps is as followings:

    first right click on the "InsertPO" method name within VSTS IDE, this will generate plumbing unit test codes as followings for this method

     

            /// <summary>
            ///A test for InsertPO (POEntity)
            ///</summary>
            [TestMethod()]
            public void InsertPOTest()
            {
                PO target = new PO();

                POEntity po = null; // TODO: Initialize to an appropriate value

                bool expected = true;
                bool actual = false;

                actual = target.InsertPO(po);

                Assert.AreEqual(expected, actual, "PO.InsertPO(POEntity) did not return the expected value.");
                //Assert.Inconclusive("Verify the correctness of this test method.");
            }

      

    As you said, I need to provide each information for the function i am testing, I was able to use some fake data to mock POEntity,but this unit test will fail when executing, I think it is that the code highlighted with red color(Database db = DatabaseFactory.CreateDatabase();) caused this unit test fail, because this code will use the connection string info I stored in web.config file to create a instance of Database class, howerver, when executing unit test, it can't get this connection string info. How should I provide such kind of information for my unit test? Actually when doing unit test, I always puzzled how to do with such circumstance. google for unit test, there are a lot of samples demonstrating how to do unit test for a method whose function is calculate sum of two addend and alike, but those samples was not so helpful I think.