Skip to content

Instantly share code, notes, and snippets.

@segilbert
Last active December 14, 2015 13:28
Show Gist options
  • Save segilbert/5093518 to your computer and use it in GitHub Desktop.
Save segilbert/5093518 to your computer and use it in GitHub Desktop.
Tell me what is wrong with this unit test? Hint ... Fails with expect count of 5 but count = 8.
public void CanExecuteNonQueryWithCommandTextWithDefinedTypeAndTransaction()
{
// Arrange
int countBefore = Convert.ToInt32(db.ExecuteScalar(countCommand));
using (DbConnection connection = db.CreateConnection())
{
connection.Open();
using (DbTransaction trans = connection.BeginTransaction())
{
// Act
db.ExecuteNonQuery(trans, CommandType.Text, insertionCommand.CommandText);
trans.Commit();
}
}
// Assert
int countAfter = Convert.ToInt32(db.ExecuteScalar(countCommand));
Assert.AreEqual(1, countAfter - countBefore);
// Cleanup
string cleanupString = "delete from Region where RegionId = 77";
DbCommand cleanupCommand = db.GetSqlStringCommand(cleanupString);
int rowsAffected = db.ExecuteNonQuery(cleanupCommand);
// Assert Cleanup
Assert.AreEqual(1, rowsAffected);
}
public void CanExecuteNonQueryWithCommandTextWithDefinedTypeAndTransaction()
{
// Arrange
using (DbConnection connection = db.CreateConnection())
{
connection.Open();
using (DbTransaction trans = connection.BeginTransaction())
{
// Act
db.ExecuteNonQuery(trans, CommandType.Text, insertionCommand.CommandText);
trans.Commit();
}
}
// Cleanup
string cleanupString = "delete from Region where RegionId = 77";
DbCommand cleanupCommand = db.GetSqlStringCommand(cleanupString);
int rowsAffected = db.ExecuteNonQuery(cleanupCommand);
// Assert
Assert.AreEqual(1, rowsAffected);
}
public void CanExecuteNonQueryWithCommandTextWithDefinedTypeAndTransaction()
{
using (DbConnection connection = db.CreateConnection())
{
connection.Open();
using (DbTransaction trans = connection.BeginTransaction())
{
db.ExecuteNonQuery(trans, CommandType.Text, insertionCommand.CommandText);
trans.Commit();
}
}
string cleanupString = "delete from Region where RegionId = 77";
DbCommand cleanupCommand = db.GetSqlStringCommand(cleanupString);
int rowsAffected = db.ExecuteNonQuery(cleanupCommand);
int count = Convert.ToInt32(db.ExecuteScalar(countCommand));
Assert.AreEqual(5, count);
Assert.AreEqual(1, rowsAffected);
}
@ornatwork
Copy link

Yes, I'm voting for the "After option"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment