tags:

views:

61

answers:

1

Folks, I am new to writing testcases for the methods. Here I have a InsertMethod for which I want to write testcase using NUnit testing framework. Help me in writing the testcase for the method below :

public bool insertUser(String FirstName, String LastName) { bool result = false;

    SqlConnection myconn = new SqlConnection();
    SqlCommand mycmd = new SqlCommand();

    try
    {
        myconn.ConnectionString = "Data Source=BABU-PC;Initial Catalog=contacts;Integrated Security=True";

        myconn.Open();

        mycmd.Connection = myconn;
        mycmd.CommandText = "InsertUser";
        mycmd.CommandType = CommandType.StoredProcedure;


        SqlParameter param1 = new SqlParameter();
        param1.ParameterName = "@FirstName";
        param1.DbType = DbType.AnsiString;
        param1.Size = 8000;
        param1.Value = FirstName;
        mycmd.Parameters.Add(param1);


        SqlParameter param2 = new SqlParameter();
        param2.ParameterName = "@LastName";
        param2.DbType = DbType.AnsiString;
        param2.Size = 8000;
        param2.Value = LastName;
        mycmd.Parameters.Add(param2);

        int i = 0;

        i = mycmd.ExecuteNonQuery();

        if (i > 0)
        {
            result = true;
        }
        else
        {
            result = false;
        }


    }

    catch (Exception err)
    {
        Console.WriteLine(err.Message.ToString());

        return false;
    }

    finally
    {
        mycmd.Dispose();
        myconn.Close();
        myconn = null;

    }
    return result;
}

Thanks

SBM

+1  A: 

Cellfish is right. Don't think about the code, think about what the method is supposed to do. Without even looking at your code, I'd do this kind of test:

1- Preparation

lastName = "LastName"
firstName = "FirstName"

Then try to fetch a user with firstName and lastName and make sure that it's not already there.

2- Execute

InsertUser(firstName, lastName)

3- Check

  • Make sure InsertUser return true
  • Try to fetch a user with firstName and lastName and make sure that it is there with the correct values.
Danny T.
This tests the 'true' return path. To test the 'false' path, attempt to insert a user that is already in the database.
Pedro
You're right! Thanks for the precision
Danny T.