所以我是单元测试,我收到了上述消息。我环顾四周,无法找到问题的答案。我不知道这是什么意思。可以有两种不同的Null吗?谢谢你的帮助!
//This is invalid input given to the CategoryList constructor. It returns a null value.
//The message i get is: Assert.AreEqual failed. Expected:<Null>.Actual:<(null)>.
CategoryList test = new CategoryList(
Primitives.ConstantPrimitives.ConnectionString,
Primitives.ConstantPrimitives.negativeShort,
false);
Assert.AreEqual(test.Cat_ID, null);
这是CategoryList
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data.SqlTypes;
namespace Retail_Utilities.Util.DataClasses
{
public class CategoryList
{
public SqlInt16 Cat_ID { get; set; }
public String Cat { get; set; }
public Boolean Active_YN { get; set; }
public SqlDateTime Last_Update { get; set; }
public Int16 Parent_Cat_ID { get; set; }
public Int16? Top_Level_Cat_ID { get; set; }
public Int16? Pallet_Type_ID { get; set; }
public CategoryList ParentCat { get; set; }
public bool IsChild { get; set; }
public CategoryList()
{
IsChild = false;
}
public CategoryList(string connectionString, Int16 catID, Boolean isChild = false)
{
IsChild = false;
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = connection.CreateCommand();
string sqlCommand = "SELECT TOP 1 Cat_ID, Cat, Active_YN, Last_Update, Parent_Cat_ID, Top_Level_Cat_ID, Pallet_Type_ID, Deactivated_On";
sqlCommand += " FROM Twr_List_Cat WHERE 0=0";
sqlCommand += " AND Cat_ID = " + catID;
command.CommandText = sqlCommand;
connection.Open();
SqlDataReader reader = command.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
IsChild = isChild;
Cat_ID = reader.GetSqlInt16(0);
Cat = reader.GetString(1);
Active_YN = reader.GetBoolean(2);
Last_Update = reader.GetSqlDateTime(3);
Parent_Cat_ID = reader.GetInt16(4);
Top_Level_Cat_ID = reader.IsDBNull(5) ? null : (Int16?)reader.GetInt16(5);
Pallet_Type_ID = reader.IsDBNull(6) ? null : (Int16?)reader.GetInt16(6);
if (!IsChild) { ParentCat = new CategoryList(connectionString, Parent_Cat_ID, true); }
}
}
reader.Close();
}
}
public static List<CategoryList> ListActiveCategories(string connectionString)
{
List<CategoryList> activeCats = new List<CategoryList>();
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand command = connection.CreateCommand())
{
command.CommandText = "SELECT Cat_ID, Cat, Active_YN, Last_Update, Parent_Cat_ID, Top_Level_Cat_ID, Pallet_Type_ID";
command.CommandText += " FROM Twr_List_Cat";
command.CommandText += " WHERE Active_YN = 1;";
connection.Open();
SqlDataReader reader = command.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
CategoryList activeCat = new CategoryList
{
Cat_ID = reader.GetSqlInt16(0),
Cat = reader.GetString(1),
Active_YN = reader.GetBoolean(2),
Last_Update = reader.GetSqlDateTime(3),
Parent_Cat_ID = reader.GetInt16(4),
Top_Level_Cat_ID = reader.IsDBNull(5) ? null : (Int16?)reader.GetInt16(5),
Pallet_Type_ID = reader.IsDBNull(6) ? null : (Int16?)reader.GetInt16(6),
};
activeCat.ParentCat = new CategoryList(connectionString, activeCat.Parent_Cat_ID, true);
activeCats.Add(activeCat);
}
}
}
}
return activeCats;
}
}
}
答案 0 :(得分:1)
是的,.Net中有几种类型的“null”
null
DBNull
并且与您所拥有的数据库相关的“没有任何内容” - SqlInt16 ToString
的类型都可以在需要时打印null
之类的内容,并可能定义比较/ .Equals
到null
以返回true
。< / LI>
根据SqlInt16
输出,看起来你手头没有值<(null)>
。
可能您正在寻找该类型的IsNull
属性:
Assert.IsTrue(test.Cat_ID.IsNull);