Unit testing with XUnit, ADO.NET, SQL Server
<p>This question is about using XUnit with ADO.NET for a To Do List app using Nancy. I'm hoping for some insight on why an <code>Assert.Equal</code> test is failing, even though the outputs appear to be identical.</p>

<p>I have a Task class with two properties: <code>id</code> and <code>description</code>.</p>

<p>The database for this app has a corresponding <code>tasks</code> table. User input is used for the description, and the <code>id</code> is an auto-incrementing identity column.</p>

<p>The <code>Task</code> class has a static List property <code>All()</code>. Each task is added to <code>All()</code>, and you can retrieve the list of all tasks by calling <code>Task.All()</code>. </p>

<p>Here's the <code>Task</code> class:</p>

<pre><code>namespace ToDoList
{
  public class Task
  {
    private int id;
    private string description;

    public Task(string Description, int Id = 0)
    {
      id = Id;
      description = Description;
    }

    public int GetId()
    {
      return id;
    }


    public string GetDescription()
    {
      return description;
    }

    public void SetDescription(string newDescription)
    {
      description = newDescription;
    }

    public static List&lt;Task&gt; All()
    {
      List&lt;Task&gt; AllTasks = new List&lt;Task&gt;{};

      SqlConnection conn = DB.Connection();
      SqlDataReader rdr = null;
      conn.Open();

      SqlCommand cmd = new SqlCommand("SELECT * FROM tasks", conn);
      rdr = cmd.ExecuteReader();

      while(rdr.Read())
      {
        int taskId = rdr.GetInt32(0);
        string taskDescription = rdr.GetString(1);
        Task newTask = new Task(taskDescription, taskId);
        AllTasks.Add(newTask);
      }

      conn.Close();

      return AllTasks;
    }

    public void Save()
    {
      SqlConnection conn = DB.Connection();
      SqlDataReader rdr;
      conn.Open();

      SqlCommand cmd = new SqlCommand("INSERT INTO tasks (description) OUTPUT INSERTED.id VALUES (@TaskDescription)", conn);

      SqlParameter testParameter = new SqlParameter();
      testParameter.ParameterName = "@TaskDescription";
      testParameter.Value = this.GetDescription();

      cmd.Parameters.Add(testParameter);

      rdr = cmd.ExecuteReader();

      while(rdr.Read())
      {
        this.id = rdr.GetInt32(0);
      }
      conn.Close();
    }

    public static void DeleteAll()
    {
      SqlConnection conn = DB.Connection();
      conn.Open();
      SqlCommand cmd = new SqlCommand("DELETE FROM tasks;", conn);
      cmd.ExecuteNonQuery();
    }
}
</code></pre>

<p>I define <code>DB.Connection</code> as the connection string in <em>Startup.cs</em>:</p>

<pre><code>...
  public class DB
  {
    public static SqlConnection Connection()
    {
      SqlConnection conn = new SqlConnection("Data Source=(localdb)\\mssqllocaldb;Initial Catalog=todo_test;Integrated Security=SSPI;");
      return conn;
    }
  }
...
</code></pre>

<p>Now here's the test I use to see if it works:</p>

<pre><code>namespace ToDoList
{
  public class ToDoTest : IDisposable
  {
    [Fact]
    public void Test_All()
    {
      //Arrange
      var description = "Wash the dog";
      var description2 = "Water the plants";
      Task testTask = new Task(description);
      testTask.Save();
      Task testTask2 = new Task(description2);
      testTask2.Save();

      //Act
      List&lt;Task&gt; result = Task.All();
      List&lt;Task&gt; testList = new List&lt;Task&gt;{testTask, testTask2};

      //Assert
      Assert.Equal(result, testList);
    }

    public void Dispose()
      {
        Task.DeleteAll();
      }
    }
}
</code></pre>

<p>The output in the console only says that the test failed:</p>

<pre><code>ToDoList.ToDoTest.Test_All [FAIL]
  Assert.Equal() Failure
  Expected: List&lt;Task&gt; [Task { }, Task { }]
  Actual:   List&lt;Task&gt; [Task { }, Task { }]
</code></pre>

<p>I did some console logs, and the <code>id</code>s and <code>description</code>s for the tasks in each list are identical.</p>

<p>In addition, when I test with:</p>

<pre><code>  List&lt;Task&gt; result = new List&lt;Task&gt;{testTask, testTask2};
  List&lt;Task&gt; testList = new List&lt;Task&gt;{testTask, testTask2};
</code></pre>

<p>The test passes.</p>

<p>I'm not really sure how to approach this to see why the test is failing. Any ideas would be great!</p>