断言失败预期:<Null> 实际:<(null)>

Assert Failed Expected:<Null> Actual:<(null)>

所以我正在进行单元测试,并且收到了上述消息。我环顾四周,找不到问题的答案。我不知道这是什么意思。可以有两种不同的 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);

这是类别列表

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;
        }
    }
}

是的,.Net中有几种"null":

  • 常规null
  • DBNull 和相关的 "nothing there yet" 数据库,例如您拥有的数据库 - SqlInt16
  • 没有值的可空类型
  • 此外,任何定义其 ToString 的类型都可以在需要时打印类似 null 的内容,并可能定义比较/.Equalsnull 到 return true.

根据 <(null)> 输出,您手头的 SqlInt16 似乎没有任何价值。

可能您正在寻找该类型的 IsNull 属性:

Assert.IsTrue(test.Cat_ID.IsNull);