From: <jmc...@gt...> - 2004-07-27 05:29:20
|
Can somebody give me a sample with .NET Mock Objects of a multiple=20 update to a database inside a transaction? I don=B4t know how to use the MockTransaction Class to verify commits=20 or rollbacks in the update. =20 =20 This is a sample of what I am trying to do. I would like had writen "InsertWidgets" method with TDD, but I could`t = got it. I dont=B4t know how to test that the inserts are made inside the = transaction, and even I have nunit frozen sometimes. =20 Other thing is how to test for the parameters of the insert sentence. I have try with MockParameterCollection but I don=B4t know how to use it =20 Thanks. Juan M. Cervera =20 =20 =20 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D using System.Collections; using System.Data; using System.Data.SqlClient; =20 namespace Sample { public class SampleProcess { =20 public static readonly string INSERTION_SQL =3D @" INSERT INTO table1 (id, name) VALUES (@id, @name) "; =20 private IDbConnection myConnection; private ArrayList myCollection =3D new ArrayList(); =20 public SampleProcess(IDbConnection cnn) { myConnection =3D cnn; =20 myCollection.Add(new Widget(1, "One")); myCollection.Add(new Widget(2, "Two")); myCollection.Add(new Widget(3, "Three")); myCollection.Add(new Widget(4, "Four")); myCollection.Add(new Widget(5, "Five")); } =20 public void InsertWidgets() { IDbTransaction trn =3D null; try { myConnection.Open(); trn =3D myConnection.BeginTransaction(); foreach (Widget widget in myCollection) { InsertOneWidget(widget, trn); } trn.Commit(); } catch { trn.Rollback(); } finally { myConnection.Close(); trn =3D null; } } =20 private void InsertOneWidget(Widget widget, IDbTransaction trn) { IDbCommand cmd =3D myConnection.CreateCommand(); cmd.Transaction =3D trn; cmd.CommandText =3D INSERTION_SQL; cmd.Parameters.Add(new SqlParameter("@Id", widget.id)); cmd.Parameters.Add(new SqlParameter("@Name", widget.name)); cmd.ExecuteNonQuery(); } } =20 public class Widget { public int id; public string name; =20 public Widget(int id, string name) { this.id =3D id; this.name =3D name; } } =20 } =20 =20 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =20 =20 using System; using DotNetMock.Framework.Data; using NUnit.Framework; using Sample; =20 namespace Sample { [TestFixture] public class TestSampleFixture { =20 private MockDbConnection cnn; =20 [SetUp] public void SetUp() { cnn =3D new MockDbConnection(); } =20 [TearDown] public void TearDown() { cnn.Verify(); } =20 =20 [Test] public void TestInsertWidgets() { =20 MockCommand cmd =3D new MockCommand(); cnn.SetExpectedCommand(cmd); cmd.SetExpectedCommandText(SampleProcess.INSERTION_SQL); cmd.SetExpectedExecuteCalls(5); =20 SampleProcess process =3D new SampleProcess(cnn); // MockTransaction txn =3D (MockTransaction) cmd.Transaction; // txn.ExpectCommitCall(true); process.InsertWidgets(); // txn.Verify(); // cmd.Verify(); =20 } =20 } =20 } =20 =20 =20 =20 =20 =20 =20 |