Reputation: 97
So I'm unit testing and I got the above message. I've looked around and cannot find an answer to my problem. I have no idea what this means. Can there be two different kinds of Null? Thanks for the help!
//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);
Here is 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;
}
}
}
Upvotes: 2
Views: 7454
Reputation: 100545
Yes, there are several types of "null" in .Net:
null
DBNull
and related "nothing there yet" for DataBases like one you have - SqlInt16ToString
to print something like null
whenever it wants and possibly defining comparisons/.Equals
to null
to return true
.It looks like you have SqlInt16
with no value at hand based on <(null)>
output.
Possibly you are looking for IsNull
property of that type instead:
Assert.IsTrue(test.Cat_ID.IsNull);
Upvotes: 1