Return to Snippet

Revision: 9747
at November 19, 2008 21:32 by rengber


Initial Code
public class TransactionStore
{
	public virtual IDataReader GetReader(SqlCommand cmd)
	{
	  return cmd.ExecuteReader(); 
	}

	public decimal GetAvailableBalance(int custNumb)
	{
	  decimal retVal = 0.00M; 
	  using (SqlConnection sqlConnection = new SqlConnection(_dbConnection))
	  {
		sqlConnection.Open();
		using (SqlCommand sqlCommand = sqlConnection.CreateCommand())
		{
		  sqlCommand.CommandType = CommandType.StoredProcedure;
		  sqlCommand.CommandText = "dbo.GetCustomerBalance";
		  sqlCommand.Parameters.AddWithValue("CustomerID", custNumb);
		  using (IDataReader dr = GetReader(sqlCommand))
		  {
			if (dr != null)
			{
			  dr.Read();
			  retVal = Convert.ToDecimal(dr["AvailableBalance"]);
			}
		  }
		}
	  }
	  return retVal; 
	}
}


[TestFixture]
public class IMTTests : TransactionStore
{
	Mockery mocks = new Mockery(); 
	IDataReader idr = null; 

	public override IDataReader GetReader(SqlCommand cmd)
	{
	  Expect.AtLeastOnce.On(idr).Method("Read").Will(Return.Value(true));
	  Expect.AtLeastOnce.On(idr).Method("Dispose"); 
	  return idr; 
	}
  
	[Test]
	public void TestAvailableBalance()
	{
	  idr = (IDataReader)mocks.NewMock(typeof(IDataReader));
	  Expect.AtLeastOnce.On(idr).Get["AvailableBalance"].Will(Return.Value(42.00M));
	  Decimal amount = this.GetAvailableBalance(12345);
	  Assert.AreEqual(42.00M, amount); 
	}
}

Initial URL

                                

Initial Description
Shows how to mock the Object["CollectionItemName"] syntax.

Initial Title
Using NMock to Create an IDataReader for Testing Data Access Methods

Initial Tags

                                

Initial Language
C#