이것은 매우 간단한 객체 지향 프로그래밍 문제입니다. 내가 뭘 하려는지 2 빈 개체를 데리고 쿼리를 사용하여 데이터베이스에서 데이터를 끌어 오기, 쿼리에서 반환 된 데이터로 빈 개체를 채 웁니다 및 다음 주장합니다. 코드 아래를 참조 객체에 대해 어설트 할 수 없습니다.
object test1;
object test2;
using (SqlConnection dbconnection = new SqlConnection(expconnstr))
{
dbconnection.Open();
var expcommand = new SqlCommand(expectedquery, dbconnection);
var actcommand = new SqlCommand(actualquery, dbconnection);
using (SqlDataReader expreader = expcommand.ExecuteReader())
{
if (expreader.Read())
{
test1 = expreader.GetValue(0);
expreader.Close();
}
}
using (SqlDataReader actreader = actcommand.ExecuteReader())
{
if (actreader.Read())
{
test2 = actreader.GetValue(0);
actreader.Close();
}
}
}
Assert.AreSame(test1, test2);
내가 같은 패턴과 잘 실행 VB 코드를 다음 VB에서 매우 유사한 시험 방법을 썼다. 그러나 VB에서 C#으로 전환하는 동안 이러한 문제가 발생합니다. 컴파일러는 어설 션에서 "할당되지 않은 변수 사용"이라고 말합니다. 그러나 위의 코드 블록 내에서 값을 할당한다고 생각했습니다. 내가 도대체 뭘 잘못하고있는 겁니까?
을 반환 (actreader, expcommand , dbconnection 등). 그들은 가치가 있고 잘 작동한다고 가정합니다. 실패한 주장입니다. – Brian