修改
Correct way to override Equals() and GetHashCode()给了我很多帮助。
以下是我的客户类的实现:
public abstract class Customer{
protected string my_edition;
protected string my_rarity;
protected ushort my_id;
protected string my_name;
public Customer() {
this.my_edition = "";
this.my_rarity = "";
this.my_id = 0;
this.my_name = "";
}
public override bool Equals(object obj) {
var item = obj as Card;
if (item == null) {
return false;
}
return this.my_edition.Equals(item.my_edition) && this.my_rarity.Equals(item.my_rarity) && this.my_id.Equals(item.my_id) && this.my_name.Equals(item.my_name);
}
public override int GetHashCode() {
return this.my_id.GetHashCode();
}
...
我的测试失败并出现错误:“集合不一样!(索引0处的元素不匹配。)”但是在调试测试和手动检查时结果是一样的。
“如果两个集合在相同的顺序和数量中具有相同的元素,则它们是相等的。如果它们的值相等,则元素相等,而不是它们引用相同的对象。元素的值使用等于默认情况下。“
我正在检查(在调试时)几次,在字符串内部甚至没有不必要的间隔。
为什么我的测试失败了? 我该如何改进我的方法?
测试方法:
[TestMethod]
public void DA_Test_GetCustomers() {
///ARRANGE
///
var cust_numbers = new Queue<int>();
cust_numbers.Enqueue(1001);
cust_numbers.Enqueue(1003);
var expected = new List<Customer> {
new Customer{
Edition = "Cust",
Rarity = "R",
ID = 1001,
Name = "John Doe",
Street = "abc",
ZIP = 54565,
Country = "abc",
Routes = new List<Route>{
new Route{
ID = 1,
Name = "Route1"
}
},
Contacts = new List<Contact>{
new Contact{
ID = 1,
Name = "Contact1"
},
new Contact{
ID = 2,
Name = "Contact2"
},
new Contact{
ID = 3,
Name = "Contact3"
}
},
Offsets = new List<Offset>{
new Offset{
ID = 2,
Name = "Offset1",
Total = 3,
GreaterThan = 0,
Operator = "&&"
},
new Offset{
ID = 6,
Name = "Offset2",
Total = 1,
GreaterThan = 0,
Operator = "&&"
},
new Offset{
ID = 9,
Name = "Offset3",
Total = 1,
GreaterThan = 0,
Operator = "&&"
},
new Offset{
ID = 16,
Name = "Offset4",
Total = 1,
GreaterThan = 0,
Operator = "&&"
},
new Offset{
ID = 29,
Name = "Offset5",
Total = 1,
GreaterThan = 40,
Operator = "XX"
}
}
},
new Customer{
Edition = "Cust",
Rarity = "R",
ID = 1003,
Name = "John Doe",
Street = "abc2",
ZIP = 54654,
Country = "abc2",
Routes = new List<Route>{
new Route{
ID = 2,
Name = "Route"
}
},
Contacts = new List<Contact>{
new Contact{
ID = 1,
Name = "Contact4"
},
new Contact{
ID = 2,
Name = "Contact5"
},
new Contact{
ID = 3,
Name = "Contact6"
}
},
Offsets = new List<Offset>{
new Offset{
ID = 4,
Name = "Offset4",
Total = 5,
GreaterThan = 0,
Operator = "||"
},
new Offset{
ID = 5,
Name = "Offset5",
Total = 4,
GreaterThan = 0,
Operator = "||"
},
new Offset{
ID = 11,
Name = "Offset6",
Total = 4,
GreaterThan = 0,
Operator = "XX"
}
}
}
};
///ACT
///
var actual = DA.GetCustomers(cust_numbers);
///ASSERT
///
Assert.AreEqual(2, actual.Count);
CollectionAssert.AreEqual(expected, actual);
}
测试方法:
public List<Customer> GetCustomers(Queue<int> cust_numbers) {
var result = new List<Customer>();
var routes = new List<Route>();
var contacts = new List<Contact>();
var offsets = new List<Offset>();
using (SqlConnection conn = new SqlConnection(ConnectionString)) {
conn.Open();
using (SqlCommand command = new SqlCommand("GetCustomer", conn)) {
command.CommandType = CommandType.StoredProcedure;
SqlParameter parameter1 = new SqlParameter();
parameter1.ParameterName = "@Edition";
parameter1.SqlDbType = SqlDbType.NChar;
parameter1.Direction = ParameterDirection.Input;
parameter1.Value = "Cust";
SqlParameter parameter2 = new SqlParameter();
parameter2.ParameterName = "ID_Cust";
parameter2.SqlDbType = SqlDbType.SmallInt;
parameter2.Direction = ParameterDirection.Input;
for (int i = cust_numbers.Count; i > 0; i-- ) {
parameter2.Value = cust_numbers.Dequeue();
command.Parameters.Add(parameter1);
command.Parameters.Add(parameter2);
using (SqlDataReader reader = command.ExecuteReader()) {
while (reader.Read()) {
routes.Add(new Route {
ID = (ushort)reader.GetInt16(0),
Name = reader.GetString(1)
});
}
reader.NextResult();
while (reader.Read()) {
contacts.Add(new Contact {
ID = (ushort)reader.GetInt16(0),
Name = reader.GetString(1)
});
}
reader.NextResult();
while (reader.Read()) {
offsets.Add(new Offset {
ID = (ushort)reader.GetInt16(0),
Name = reader.GetString(1),
Total = reader.GetByte(2),
GreaterThan = reader.GetByte(3),
Operator = reader.GetString(4)
});
}
reader.NextResult();
while (reader.Read()) {
result.Add(new Customer {
Edition = reader.GetString(0),
Rarity = reader.GetString(1),
ID = (ushort)reader.GetInt16(2),
Name = reader.GetString(3),
Street = reader.GetByte(4),
ZIP = reader.GetByte(5),
Country = reader.GetString(6),
Routes = new List<Route>(routes),
Contacts = new List<Contact>(contacts),
Offsets = new List<Offset>(offsets)
});
}
routes.Clear();
contacts.Clear();
offsets.Clear();
command.Parameters.Clear();
}
};
}
}
return result;
}