Skip to content

Instantly share code, notes, and snippets.

@segilbert
Last active Dec 14, 2015
Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@ornatwork ornatwork commented Mar 8, 2013

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