chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
0 votes
I am trying to mock some transactions in sql using the following code:

         //Type type = typeof(SqlTransaction);
         //ConstructorInfo[] info = type.GetConstructors(BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Instance);
         //Object obj = info[0].Invoke(new object[] { new SqlInternalConnection, null, IsolationLevel.ReadCommitted, null });

         SqlTransaction sqlTransaction = (SqlTransaction)MockManager.MockObject(typeof(SqlTransaction)).Object;

         Mock baseMock = MockManager.Mock(typeof(Base));
         baseMock.ExpectGetAlways("ConnectionString", "");

         Mock sqlConnectionMock = MockManager.Mock(typeof(SqlConnection));
         sqlConnectionMock.ExpectCall("Open");
         sqlConnectionMock.ExpectAndReturn("BeginTransaction", sqlTransaction).Args(Check.IsTypeOf(typeof(IsolationLevel)), Check.IsTypeOf(typeof(string)));
         sqlConnectionMock.ExpectCall("Close");
         sqlConnectionMock.ExpectCall("Dispose");

         Mock sqlTransactionMock = MockManager.Mock(typeof(SqlTransaction));
         sqlTransactionMock.ExpectCall("Commit");


My problem is I can't figure out how to create an instance of SqlTransaction since all of it's contructors are private, and even if I call the constructor with reflection, some of the arguments it requires are private.

Any suggestions?
asked by goodrich (640 points)

1 Answer

0 votes
I worked around this by using the System.Transactions namespace to automatically wrap my calls in transactions where required. This was easier to Mock.

Now I'm having some other problems, but I'll save that for another post if I can't figure it out.
answered by goodrich (640 points)
...