Beispiel #1
0
        /// <summary>
        /// Default constructor.
        /// </summary>
        public SessionsControllerTestHarness(bool shouldOpenDatabaseConnection, bool shouldCommitDatabaseTransaction)
        {
            // Build the mocked database objects.
            this.MockedDatabaseTransaction        = new Mock <IDatabaseTransaction>(MockBehavior.Strict);
            this.MockedDatabaseConnection         = new Mock <IDatabaseConnection>(MockBehavior.Strict);
            this.MockedDatabaseConnectionProvider = new Mock <IDatabaseConnectionProvider>(MockBehavior.Strict);

            // Mock the database connection.
            if (shouldOpenDatabaseConnection)
            {
                // Verify the database connection is opened.
                this.MockedDatabaseConnectionProvider
                .Setup(mock => mock.OpenDatabaseConnection())
                .Returns(Task.FromResult <IDatabaseConnection>(this.MockedDatabaseConnection.Object))
                .Verifiable();

                // Verify the database transaction begins.
                this.MockedDatabaseConnection
                .Setup(mock => mock.BeginDatabaseTransaction())
                .Returns(this.MockedDatabaseTransaction.Object)
                .Verifiable();

                // Verify the database transaction is disposed.
                this.MockedDatabaseTransaction
                .Setup(mock => mock.Dispose())
                .Verifiable();

                // Verify the database connection is disposed.
                this.MockedDatabaseConnection
                .Setup(mock => mock.Dispose())
                .Verifiable();
            }

            // Mock the database transaction.
            if (shouldCommitDatabaseTransaction)
            {
                // Verify the database transaction is committed.
                this.MockedDatabaseTransaction
                .Setup(mock => mock.Commit())
                .Verifiable();
            }

            // Build the mocked Scheduling business logic component.
            this.MockedSchedulingBusinessLogicComponent = new Mock <ISchedulingBusinessLogicComponent>(MockBehavior.Strict);

            // Build the Sessions controller.
            this.SessionsController = new SessionsController(this.MockedDatabaseConnectionProvider.Object, this.MockedSchedulingBusinessLogicComponent.Object);

            // Build the HTTP configuration.
            HttpConfiguration httpConfiguration = new HttpConfiguration();

            GlobalHttpApplication.ConfigureApplication(httpConfiguration);

            // Build the mocked dependency resolver.
            MockedDependencyResolver mockedDependencyResolver = new MockedDependencyResolver();

            mockedDependencyResolver.Setup(mock => mock.GetService(typeof(SessionsController)))
            .Returns(this.SessionsController);

            // Set the mocked dependency resolver.
            httpConfiguration.DependencyResolver = mockedDependencyResolver.Object;

            // Build the in memory HTTP server.
            HttpServer httpServer = new HttpServer(httpConfiguration);

            // Build the HTTP client.
            this.HttpClient = new HttpClient(httpServer);
        }
        /// <summary>
        /// Default constructor.
        /// </summary>
        public SessionsControllerTestHarness(bool shouldOpenDatabaseConnection, bool shouldCommitDatabaseTransaction)
        {
            // Build the mocked database objects.
            this.MockedDatabaseTransaction = new Mock<IDatabaseTransaction>(MockBehavior.Strict);
            this.MockedDatabaseConnection = new Mock<IDatabaseConnection>(MockBehavior.Strict);
            this.MockedDatabaseConnectionProvider = new Mock<IDatabaseConnectionProvider>(MockBehavior.Strict);

            // Mock the database connection.
            if (shouldOpenDatabaseConnection)
            {
                // Verify the database connection is opened.
                this.MockedDatabaseConnectionProvider
                    .Setup(mock => mock.OpenDatabaseConnection())
                    .Returns(Task.FromResult<IDatabaseConnection>(this.MockedDatabaseConnection.Object))
                    .Verifiable();

                // Verify the database transaction begins.
                this.MockedDatabaseConnection
                    .Setup(mock => mock.BeginDatabaseTransaction())
                    .Returns(this.MockedDatabaseTransaction.Object)
                    .Verifiable();

                // Verify the database transaction is disposed.
                this.MockedDatabaseTransaction
                    .Setup(mock => mock.Dispose())
                    .Verifiable();

                // Verify the database connection is disposed.
                this.MockedDatabaseConnection
                    .Setup(mock => mock.Dispose())
                    .Verifiable();
            }

            // Mock the database transaction.
            if (shouldCommitDatabaseTransaction)
            {
                // Verify the database transaction is committed.
                this.MockedDatabaseTransaction
                    .Setup(mock => mock.Commit())
                    .Verifiable();
            }

            // Build the mocked Scheduling business logic component.
            this.MockedSchedulingBusinessLogicComponent = new Mock<ISchedulingBusinessLogicComponent>(MockBehavior.Strict);

            // Build the Sessions controller.
            this.SessionsController = new SessionsController(this.MockedDatabaseConnectionProvider.Object, this.MockedSchedulingBusinessLogicComponent.Object);

            // Build the HTTP configuration.
            HttpConfiguration httpConfiguration = new HttpConfiguration();
            GlobalHttpApplication.ConfigureApplication(httpConfiguration);

            // Build the mocked dependency resolver.
            MockedDependencyResolver mockedDependencyResolver = new MockedDependencyResolver();
            mockedDependencyResolver.Setup(mock => mock.GetService(typeof(SessionsController)))
                .Returns(this.SessionsController);

            // Set the mocked dependency resolver.
            httpConfiguration.DependencyResolver = mockedDependencyResolver.Object;

            // Build the in memory HTTP server.
            HttpServer httpServer = new HttpServer(httpConfiguration);

            // Build the HTTP client.
            this.HttpClient = new HttpClient(httpServer);
        }