huijuan0501 发表于 2009-8-27 15:45:06

VS 中的unit test

最近遇到些困难,利用vs中的unit tet 进行对数据库设计代码测试,这个虽然自动生成了测试框架,但里面一些还是不会,比如返回DataSet expected =DataSet actual 这了返回数据怎么来做

部分源码
/// 返回数据集
      /// </summary>
      /// <param name="commandText"></param>
      /// <param name="commandType"></param>
      /// <param name="sqlParameters"></param>
      /// <returns></returns>
      public DataSet ExecuteQuery(string commandText, CommandType commandType, SqlParameter[] sqlParameters)
      {
            DataSet ds = new DataSet();

            using (SqlCommand cmd = new SqlCommand())
            {

                PrepareConnection(commandText, commandType, cmd, sqlParameters);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                try
                {
                  da.Fill(ds);
                }
                catch (Exception e)
                {
                  e.StackTrace.ToString();
                }
            }
            return ds;
      }

      /// <summary>
      /// 参数预编译
      /// </summary>
      /// <param name="commandText"></param>
      /// <param name="commandType"></param>
      /// <param name="cmd"></param>
      /// <param name="sqlParameters"></param>
      private void PrepareConnection(string commandText, CommandType commandType, SqlCommand cmd, SqlParameter[] sqlParameters)
      {
            SqlConnection conn = new SqlConnection(DbConnectionString);

            if (conn.State != ConnectionState.Open)
            {
                try
                {
                  conn.Open();
                }
                catch (Exception e)
                {
                  e.StackTrace.ToString();
                }
            }
            cmd.CommandText = commandText;
            cmd.Connection = conn;
            cmd.CommandType = commandType;

            if (sqlParameters.Length >= 1)
            {
                foreach (SqlParameter sqlParameter in sqlParameters)
                {
                  if (sqlParameter.Value == null)
                  {
                        sqlParameter.Value = string.Empty;
                  }
                  cmd.Parameters.AddWithValue(sqlParameter.ParameterName, sqlParameter.Value);
                }
            }
      }
    }
}
生成测试代码
/// <summary>
      ///A test for ExecuteQuery
      ///</summary>
      
      public void ExecuteQueryTest()
      {
            DBAccessor target = new DBAccessor(); // TODO: Initialize to an appropriate value
            string commandText =@" SELECT * from UserData where ='tt'" ; // TODO: Initialize to an appropriate value
            CommandType commandType = CommandType.Text; // TODO: Initialize to an appropriate value
            SqlParameter[] sqlParameters = new SqlParameter[]{}; // TODO: Initialize to an appropriate value
            DataSet expected =; // TODO: Initialize to an appropriate value
            DataSet actual;
            actual = target.ExecuteQuery(commandText, commandType, sqlParameters);
            Assert.AreEqual(expected, actual);
            Assert.Inconclusive("Verify the correctness of this test method.");
      }

      /// <summary>
      ///A test for PrepareConnection
      ///</summary>
      
      
      public void PrepareConnectionTest()
      {
            DBAccessor_Accessor target = new DBAccessor_Accessor(); // TODO: Initialize to an appropriate value
            string commandText = string.Empty; // TODO: Initialize to an appropriate value
            CommandType commandType = new CommandType(); // TODO: Initialize to an appropriate value
            SqlCommand cmd = null; // TODO: Initialize to an appropriate value
            SqlParameter[] sqlParameters = null; // TODO: Initialize to an appropriate value
            target.PrepareConnection(commandText, commandType, cmd, sqlParameters);
            Assert.Inconclusive("A method that does not return a value cannot be verified.");
      }

      /// <summary>
      ///A test for DbConnectionString
      ///</summary>
      
      
      public void DbConnectionStringTest()
      {
            string actual;
            actual = DBAccessor_Accessor.DbConnectionString;
            Assert.Inconclusive("Verify the correctness of this test method.");
      }
    }
}
中的string commandText = string.Empty 等参数怎么进行设置呢?

huijuan0501 发表于 2009-8-28 11:07:03

那位会的话帮我看看这个问题哦

那位会的话帮我看看这个问题哦。先行谢过啦

卡朵 发表于 2009-9-15 14:05:09

我也是碰到这个问题 我判断它返回的不为空就行 我也觉得很牵强 大家一起交流吧

huijuan0501 发表于 2009-9-16 20:42:36

没 ,这个问题我到现在还不明白的,事情多,我没解决这个问题
页: [1]
查看完整版本: VS 中的unit test