public void CanAddParameter()
        {
            var paramNameYear  = "year";
            var paramValueYear = "1990";

            Expression <Func <DbParameterCollection, int> > ParametersAddTestExpression(string paramName, string paramValue1)
            {
                return(x => x.Add(It.Is <DbParameter>(y => y.ParameterName == paramName && (string)y.Value == paramValue1)));
            }

            MockDatabaseFactory.DbParameter  = MockDatabaseFactory.CreateDbParameter();
            MockDatabaseFactory.Parameters   = new Mock <DbParameterCollection>();
            MockDatabaseFactory.DbConnection = MockDatabaseFactory.CreateDbConnection();
            MockDatabaseFactory.DbCommand    = MockDatabaseFactory.CreateDbCommand();

            MockDatabaseFactory.Parameters.Setup(
                ParametersAddTestExpression(paramNameYear, paramValueYear));

            var yearParam = MockDatabaseFactory.CreateDbParameter();

            MockProcedureBuilder.Setup(x => x.CreateParameter())
            .Returns(yearParam.Object);

            MockProcedureBuilder.Object.Command             = MockDatabaseFactory.DbCommand.Object;
            MockProcedureBuilder.Object.Command.CommandText = StoredProcName;
            MockProcedureBuilder.Object.Command.CommandType = CommandType.StoredProcedure;

            MockProcedureBuilder.Object.LoadStoredProc(Context, StoredProcName, false);
            MockProcedureBuilder.Object.AddParameter(paramNameYear, paramValueYear);
            MockDatabaseFactory.Parameters.Verify(ParametersAddTestExpression(paramNameYear, paramValueYear), Times.Once);
        }
Beispiel #2
0
        public void GivenManageConnectionTrueAndConnectionIsOpenDoesNotTryToReOpenConnection()
        {
            var reader = new Mock <DbDataReader>();

            MockDatabaseFactory.DbParameter  = MockDatabaseFactory.CreateDbParameter();
            MockDatabaseFactory.Parameters   = new Mock <DbParameterCollection>();
            MockDatabaseFactory.DbConnection = MockDatabaseFactory.CreateDbConnection();
            MockDatabaseFactory.DbCommand    = MockDatabaseFactory.CreateDbCommand(reader.Object);
            MockDatabaseFactory.DbConnection.SetupGet(x => x.State)
            .Returns(ConnectionState.Open);

            // SetupGet<ConnectionState>("State")
            // .Returns(ConnectionState.Open);
            var calledHandler = false;
            var handleResults =
                new Action <IResultReader>(x =>
            {
                calledHandler = true;
            });

            var MockProcedureBuilder = new Mock <ProcedureBuilder>();

            MockProcedureBuilder.Object.Command = MockDatabaseFactory.DbCommand.Object;
            MockProcedureBuilder.Object.Execute(handleResults, manageConnection: true);
            MockDatabaseFactory.DbConnection.Verify(x => x.Open(), Times.Never);
            Assert.True(calledHandler);
        }
 public void CallingAddParameterWithoutCallingLoadStoredProcFristThrowsError()
 {
     MockDatabaseFactory.DbParameter     = MockDatabaseFactory.CreateDbParameter();
     MockDatabaseFactory.Parameters      = new Mock <DbParameterCollection>();
     MockDatabaseFactory.DbConnection    = MockDatabaseFactory.CreateDbConnection();
     MockDatabaseFactory.DbCommand       = MockDatabaseFactory.CreateDbCommand();
     MockProcedureBuilder.Object.Command = MockDatabaseFactory.DbCommand.Object;
     Assert.Throws <InvalidOperationException>(() => MockProcedureBuilder.Object.AddParameter("year", "2000"));
 }
Beispiel #4
0
        public void GivenManageConnectionFalseDoesNotTryToReOpenConnection()
        {
            var reader = new Mock <DbDataReader>();

            MockDatabaseFactory.DbParameter  = MockDatabaseFactory.CreateDbParameter();
            MockDatabaseFactory.Parameters   = new Mock <DbParameterCollection>();
            MockDatabaseFactory.DbConnection = MockDatabaseFactory.CreateDbConnection();
            MockDatabaseFactory.DbCommand    = MockDatabaseFactory.CreateDbCommand(reader.Object);

            var calledHandler = false;
            var handleResults =
                new Action <IResultReader>(x =>
            {
                calledHandler = true;
            });

            var MockProcedureBuilder = new Mock <ProcedureBuilder>();

            MockProcedureBuilder.Object.Command = MockDatabaseFactory.DbCommand.Object;
            MockProcedureBuilder.Object.Execute(handleResults, manageConnection: false);
            MockDatabaseFactory.DbConnection.Verify(x => x.Open(), Times.Never);
            Assert.True(calledHandler);
        }
Beispiel #5
0
        public void ExecuteCallHandlerAndClosesConnection()
        {
            var reader = new Mock <DbDataReader>();

            MockDatabaseFactory.DbParameter  = MockDatabaseFactory.CreateDbParameter();
            MockDatabaseFactory.Parameters   = new Mock <DbParameterCollection>();
            MockDatabaseFactory.DbConnection = MockDatabaseFactory.CreateDbConnection();
            MockDatabaseFactory.DbCommand    = MockDatabaseFactory.CreateDbCommand(reader.Object);

            var calledHandler = false;
            var handleResults =
                new Action <IResultReader>(x =>
            {
                calledHandler = true;
            });

            var MockProcedureBuilder = new Mock <ProcedureBuilder>();

            MockProcedureBuilder.Object.Command = MockDatabaseFactory.DbCommand.Object;
            MockProcedureBuilder.Object.Execute(handleResults);
            MockDatabaseFactory.DbConnection.Verify(x => x.Close(), Times.Once);
            Assert.True(calledHandler);
        }