Ejemplo n.º 1
0
        protected IDataMapper CreateDB_ForInsert()
        {
            _parameters = MockRepository.GenerateMock<DbParameterCollection>();

            _command = MockRepository.GenerateMock<DbCommand>();
            _command.Expect(c => c.Parameters).Return(_parameters);
            _command.Stub(c => c.CommandText);
            _command
                .Expect(c => c.CreateParameter())
                .Repeat.Any()
                .Return(MockRepository.GenerateStub<DbParameter>());

            _connection = MockRepository.GenerateMock<DbConnection>();
            _connection.Expect(c => c.CreateCommand()).Return(_command);

            _command.Expect(c => c.Connection).Return(_connection);

            DbProviderFactory dbFactory = MockRepository.GenerateMock<DbProviderFactory>();
            dbFactory.Expect(f => f.CreateConnection()).Return(_connection);

            return new DataMapper(dbFactory, "connString...");
        }
Ejemplo n.º 2
0
        protected IDataMapper CreateDB_ForQuery(StubResultSet rs)
        {
            StubDataReader reader = new StubDataReader(rs);

            _parameters = MockRepository.GenerateMock<DbParameterCollection>();
            _parameters.Expect(p => p.Add(null)).Return(1).IgnoreArguments();

            _command = MockRepository.GenerateMock<DbCommand>();
            _command.Expect(c => c.ExecuteReader()).Return(reader);
            
            _command.Expect(c => c.Parameters).Return(_parameters);
            _command.Expect(c => c.CreateParameter()).Return(new System.Data.SqlClient.SqlParameter()).Repeat.Any();
            _command.Stub(c => c.CommandText);

            _connection = MockRepository.GenerateMock<DbConnection>();
            _connection.Expect(c => c.CreateCommand()).Return(_command);

            _command.Expect(c => c.Connection).Return(_connection);

            DbProviderFactory dbFactory = MockRepository.GenerateMock<DbProviderFactory>();
            dbFactory.Expect(f => f.CreateConnection()).Return(_connection);

            return new DataMapper(dbFactory, "connString...");
        }
        private void Setup()
        {
            parametersList = new List<DbParameter>();

            target = new DatabaseTraceListener_Accessor(
            storedProcedureName, logConnectionStringName, null, null);

            //target.connectionStringName = "TestConnectionString";

            target.factory = MockRepository.GenerateStub<DbProviderFactory>();
            target.connectionStringProvider = MockRepository.GenerateStub<IConfigurationValueProvider>();
            target.connectionStringProvider.Stub((p) => p[target.connectionStringName]).
                Return(target.connectionStringName);

            connection = MockRepository.GenerateStub<DbConnection>();
            command = MockRepository.GenerateStub<DbCommand>();
            parameters = MockRepository.GenerateStub<DbParameterCollection>();

            // Stubs for the connection and command
            target.factory.Stub(
                (f) => f.CreateConnection()).Return(connection);
            target.factory.Stub((f) => f.CreateCommand()).Return(command);

            // Definitely want to verify that Open is called
            connection.Stub((c) => connection.Open());
            // Unfortunately can't verify Dispose got called
            connection.Stub((c) => connection.Dispose());
            // Will verify later if ExecuteNonQuery is called
            command.Stub((c) => command.ExecuteNonQuery()).Return(1);
            command.Stub((c) => c.Dispose());
            // Setup repetitive stub for the command parameters
            //DbParameter parameter = null;

            command.Stub((c) => c.Parameters).Repeat.Any().Return(parameters);

            for (int i = 0; i < 12; i++)
            {
                DbParameter parameter = MockRepository.GenerateStub<DbParameter>();
                target.factory.Stub((f) => f.CreateParameter()).Return(parameter);
                parameters.Stub((p) => p.Add(parameter)).Do(new HandleParameterDelegate(HandleParameter));
            }
        }