Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am writing a unit test case for my application.In my application, I am using SQLite datebase.I want to mock following methods.Like,ExecuteNonQuery, ExecuteScalar, GetDataSet.I am writing my methods below.

Please, any one help me to write mock unit test case for following method.

ExecuteNonQuery
C#
using (SQLiteConnection objCon = objISQLite.OpenConnection(DBNAME))
{
     objCon.Open();
     using (SQLiteCommand objCmd = new SQLiteCommand(strSQL, objCon))
     {
           intRetValue = objCmd.ExecuteNonQuery();
           objCmd.Dispose();
     }
     objCon.Close();
     objCon.Dispose();
}

ExecuteScalar
C#
using (SQLiteConnection objCon = OpenConnection(DBNAME))
{
      objCon.Open();
      using (SQLiteCommand objCmd = new SQLiteCommand(strSQL, objCon))
      {
            objRetValue = objCmd.ExecuteScalar();
            objCmd.Dispose();
      }
      objCon.Close();
      objCon.Dispose();
}

GetDataSet
C#
using (DataSet dsData = new DataSet())
{
      using (SQLiteConnection objCon = OpenConnection(DBNAME))
      {
           objCon.Open();
           using (SQLiteDataAdapter objAdp = new SQLiteDataAdapter(strSQL,objCon))
                 {
                       dsData.Reset();
                       objAdp.Fill(dsData);
                       objAdp.Dispose();
                 }
                 objCon.Close();
                 objCon.Dispose();
      }
      return (#Dataset);
}
Posted

1 solution

That code isn't really unit-testable. When you unit test you have to convert your code into services that are accessed via interfaces and it is those services\interfaces that you moq. So create an IMyData interface with a GetJobs method, then create a MyData class that implements this interface and implements the method

C#
public class MyData : IMyData
{
    public DataSet GetJobs()
    {
        using (DataSet dsData = new DataSet())
        {
            using (SQLiteConnection objCon = OpenConnection(DBNAME))
            {
                objCon.Open();
                using (SQLiteDataAdapter objAdp = new SQLiteDataAdapter(strSQL,objCon))
                {
                    dsData.Reset();
                    objAdp.Fill(dsData);
                    objAdp.Dispose();
                }

                objCon.Close();
                objCon.Dispose();
            }
            return (#Dataset);
        }
    }
}


Your code then uses something of type IMyData to get the jobs;

C#
public class MyBusinessLogic
{
    private IMyData myData;
    
    public MyBusinessLogic() : this (new MyData())
    {
        
    }

    public MyBusinessLogic(IMyData myData)
    {
        this.myData = mydata;
    }

    public int MyFunction(DateTime dt);
    { 
        DataSet jobs = this.myData.GetJobs();

        // get the jobs that were made before the given date
        return jobs.Count(j => j.Created <= dt);
    }
}


You now have two constructors for MyBusinessLogic, one that your normal code uses;

MyBusinessLogic bl = new MyBusinessLogic();


and one that your unit tests will use. So in your unit test you write a moq for IMyData and have IMyData.GetJobs return a hard-coded DataSet with some sample data in it and your unit test passes this mocked object to your MyBusinessLogic class so that class can be tested;

C#
MyBusinessLogic bl = new MyBusinessLogic(myMockedIMyData.ojbect);
int x = bl.MyFunction(new DateTime(2015, 1, 1));


Now you can test the login inside your MyFunction is correct for your given inputs versus the data you have mocked the interface to return.

I appreciate this is a massive paradigm shift and could result in you re-writing a lot of code, but these are the things you need to do to make your code unit-testable. You can't write any code you want and it is automatically unit-testable (if you want that then look to Microsoft Fakes).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900