Ejemplo n.º 1
0
        public void ShouldExecuteUnderAmbientDistributedTransactionAndDataSourceTransactionCompleteTest()
        {
            Mockery mockery;
            AdoNetDataSource dataSource;
            IConnectionFactory mockConnectionFactory;
            IDbConnection mockDbConnection;
            IDbCommand mockDbCommand;
            IDataParameterCollection mockDataParameterCollection;
            IDataParameter[] mockDataParameters;
            IDataParameter mockDataParameter0;
            IDataParameter mockDataParameter1;

            int recordsAffected;

            mockery = new Mockery();
            mockConnectionFactory = mockery.NewMock<IConnectionFactory>();
            mockDbConnection = mockery.NewMock<IDbConnection>();
            mockDbCommand = mockery.NewMock<IDbCommand>();
            mockDataParameterCollection = mockery.NewMock<IDataParameterCollection>();
            mockDataParameter0 = mockery.NewMock<IDataParameter>();
            mockDataParameter1 = mockery.NewMock<IDataParameter>();
            mockDataParameters = new IDataParameter[] { mockDataParameter0, mockDataParameter1 };

            Expect.Once.On(mockConnectionFactory).Method("GetConnection").Will(Return.Value(mockDbConnection));
            Expect.Once.On(mockConnectionFactory).GetProperty("ConnectionType").Will(Return.Value(mockDbConnection.GetType()));
            //Expect.AtLeastOnce.On(mockDbConnection).GetProperty("State").Will(Return.Value(ConnectionState.Open));
            Expect.AtLeastOnce.On(mockDbConnection).Method("Dispose").WithNoArguments();
            Expect.AtLeastOnce.On(mockDbConnection).SetProperty("ConnectionString").To("myConnectionString");
            Expect.AtLeastOnce.On(mockDbConnection).Method("Open").WithNoArguments();
            Expect.AtLeastOnce.On(mockDbConnection).Method("CreateCommand").WithNoArguments().Will(Return.Value(mockDbCommand));
            Expect.AtLeastOnce.On(mockDbCommand).GetProperty("Parameters").Will(Return.Value(mockDataParameterCollection));
            Expect.AtLeastOnce.On(mockDataParameterCollection).Method("Clear").WithNoArguments();
            Expect.AtLeastOnce.On(mockDbCommand).Method("Dispose").WithNoArguments();
            Expect.AtLeastOnce.On(mockDbCommand).SetProperty("Connection").To(mockDbConnection);
            Expect.AtLeastOnce.On(mockDbCommand).SetProperty("CommandType").To(CommandType.StoredProcedure);
            Expect.AtLeastOnce.On(mockDbCommand).SetProperty("CommandText").To("blah blah blah");
            Expect.AtLeastOnce.On(mockDbCommand).SetProperty("Transaction");
            Expect.AtLeastOnce.On(mockDbCommand).Method("ExecuteNonQuery").WithNoArguments().Will(Return.Value(1));

            Expect.AtLeastOnce.On(mockDataParameter0).GetProperty("Value").Will(Return.Value(1));
            Expect.AtLeastOnce.On(mockDataParameter1).GetProperty("Value").Will(Return.Value(null));
            Expect.AtLeastOnce.On(mockDataParameter1).SetProperty("Value").To(DBNull.Value);
            Expect.AtLeastOnce.On(mockDataParameterCollection).Method("Add").With(mockDataParameter0).Will(Return.Value(0));
            Expect.AtLeastOnce.On(mockDataParameterCollection).Method("Add").With(mockDataParameter1).Will(Return.Value(0));

            using (TransactionScope ts = new TransactionScope())
            {
                using (DataSourceTransactionScope dsts = new DataSourceTransactionScope())
                {
                    dataSource = new AdoNetDataSource(MOCK_CONNECTION_STRING, mockConnectionFactory);

                    recordsAffected = dataSource.ExecuteNonQuery(CommandType.StoredProcedure, "blah blah blah", mockDataParameters, null, false);

                    dsts.Complete();
                    ts.Complete();
                }
            }

            Assert.AreEqual(1, recordsAffected);

            mockery.VerifyAllExpectationsHaveBeenMet();
        }
Ejemplo n.º 2
0
        public void ShouldGetConnectionAndTransactionUnderAmbientDistributedTransactionAndDataSourceTransactionTest()
        {
            Mockery mockery;
            MockAdoNetAmbientAware ambientAware;
            IConnectionFactory mockConnectionFactory;
            IDbConnection mockDbConnection;

            IDbConnection dbConnection;
            IDbTransaction dbTransaction;

            mockery = new Mockery();
            mockConnectionFactory = mockery.NewMock<IConnectionFactory>();
            mockDbConnection = mockery.NewMock<IDbConnection>();

            Expect.Once.On(mockConnectionFactory).Method("GetConnection").Will(Return.Value(mockDbConnection));
            Expect.Once.On(mockConnectionFactory).GetProperty("ConnectionType").Will(Return.Value(mockDbConnection.GetType()));
            //Expect.AtLeastOnce.On(mockDbConnection).GetProperty("State").Will(Return.Value(ConnectionState.Open));
            Expect.AtLeastOnce.On(mockDbConnection).Method("Dispose").WithNoArguments();
            Expect.AtLeastOnce.On(mockDbConnection).SetProperty("ConnectionString").To("myConnectionString");
            Expect.AtLeastOnce.On(mockDbConnection).Method("Open").WithNoArguments();

            ambientAware = new MockAdoNetAmbientAware(MOCK_CONNECTION_STRING, mockConnectionFactory);

            Assert.True(ambientAware.IsShouldDisposeResources);
            using (TransactionScope ts = new TransactionScope())
            {
                Assert.IsTrue(ambientAware.IsShouldDisposeResources);
                using (DataSourceTransactionScope dsts = new DataSourceTransactionScope())
                {
                    Assert.IsFalse(ambientAware.IsShouldDisposeResources);
                    ambientAware.BypassGetConnectionAndTransaction(out dbConnection, out dbTransaction);

                    Assert.IsNotNull(dbConnection);
                    Assert.IsNull(dbTransaction);
                }
                Assert.True(ambientAware.IsShouldDisposeResources);
            }
            Assert.True(ambientAware.IsShouldDisposeResources);

            mockery.VerifyAllExpectationsHaveBeenMet();
        }