コード例 #1
0
        public void DeleteCustomerDeletesCustomer()
        {
            //Arrange:
            // Instantiate CustomersController so its methods can be called
            // Create a new customer to be deleted, and get its customer ID
            var customerController = new CustomersController();

            var customer = new CustomerModel
            {
                FirstName = "Jim",
                LastName = "McDonald",
                Address1 = "Farm",
                Address2 = "Yard",
                City = "Denver",
                State = "CO",
                Zip = "56432"
            };
            IHttpActionResult result = customerController.PostCustomer(customer);
            CreatedAtRouteNegotiatedContentResult<CustomerModel> contentResult =
                (CreatedAtRouteNegotiatedContentResult<CustomerModel>)result;

            int customerIdToDelete = contentResult.Content.CustomerId;

            //Act: Call DeleteCustomer
            result = customerController.DeleteCustomer(customerIdToDelete);

            //Assert:
            // Verify that HTTP result is OK
            // Verify that reading deleted customer returns result not found
            Assert.IsInstanceOfType(result, typeof(OkNegotiatedContentResult<Customer>));

            result = customerController.GetCustomer(customerIdToDelete);
            Assert.IsInstanceOfType(result, typeof(NotFoundResult));
        }
コード例 #2
0
        public void PostCustomerCreatesCustomer()
        {
            //Arrange
            var customerController = new CustomersController();

            //Act
            var newCustomer = new CustomerModel
            {
                FirstName = "Testy",
                LastName = "McTesterson",
                Address1 = "123 Main Street",
                Address2 = "Suite 2",
                City = "San Diego",
                States = "CA",
                Zip = "92101"
            };

            //The result of the Post Request
            IHttpActionResult result = customerController.PostCustomer(newCustomer);

            //Assert

            //If not 'true' Assert False
            Assert.IsInstanceOfType(result, typeof(CreatedAtRouteNegotiatedContentResult<CustomerModel>));

            //Cast
            CreatedAtRouteNegotiatedContentResult<CustomerModel> contentResult = (CreatedAtRouteNegotiatedContentResult<CustomerModel>)result;

            //Check if Customer is posted to the database
            //Check to see if Customer ID is NOT equal to zero.  If Customer Id us equal to zero,
            //then customer was NOT added to Database
            Assert.IsTrue(contentResult.Content.CustomerId != 0);
        }
コード例 #3
0
        public void GetCustomersReturnsCustomers()
        {
            //Arrange
            //Properties go here
            var customerController = new CustomersController();

            //Act
            //Call the method in question that needs to be tested
            IEnumerable<CustomerModel> customers = customerController.GetCustomers();


            //Assert
            //You assert that if the outcome is this, assert that.
            Assert.IsTrue(customers.Count() > 0);
        }
コード例 #4
0
        public void GetCustomerReturnsCustomer()
        {
            int customerIdForTest = 1;

            //Arrange: Instantiate CustomersController so its methods can be called
            var customerController = new CustomersController();

            //Act: Call the GetCustomer method
            IHttpActionResult result = customerController.GetCustomer(customerIdForTest);

            //Assert:
            // Verify that HTTP status code is OK
            // Verify that returned customer ID is correct
            Assert.IsInstanceOfType(result, typeof(OkNegotiatedContentResult<CustomerModel>));

            OkNegotiatedContentResult<CustomerModel> contentResult =
                (OkNegotiatedContentResult<CustomerModel>)result;
            Assert.IsTrue(contentResult.Content.CustomerId == customerIdForTest);
        }
コード例 #5
0
        public void GetAccountsForCustomerReturnsAccounts()
        {
            int customerIdForTest = 1;

            // Arrange: Instantiate CustomersController so its methods can be called
            var customerController = new CustomersController();

            //Act: Call the GetAccountsForCustomer method
            IHttpActionResult result =
                customerController.GetAccountsForCustomer(customerIdForTest);

            //Assert:
            // Verify that HTTP status code is OK
            // Verify that an array was returned with at least one element
            Assert.IsInstanceOfType(result,
                typeof(OkNegotiatedContentResult<IQueryable<AccountModel>>));

            OkNegotiatedContentResult<IQueryable<AccountModel>> contentResult =
                (OkNegotiatedContentResult<IQueryable<AccountModel>>)result;
            Assert.IsTrue(contentResult.Content.Count() > 0);
        }
コード例 #6
0
        public void PutCustomerUpdatesCustomer()
        {
            //Arrange
            var cusomersContoller = new CustomersController();

            var newCustomer = new CustomerModel
            {
                FirstName = "Testy",
                LastName = "McTesterson",
                Address1 = "123 Main Street",
                Address2 = "Suite 2",
                City = "San Diego",
                States = "CA",
                Zip = "92101"
            };

            //The result of the Post Request
            IHttpActionResult result = cusomersContoller.PostCustomer(newCustomer);

            //Cast result as Content Result so that I can gather information from ContentResult
            CreatedAtRouteNegotiatedContentResult<CustomerModel> contentResult = (CreatedAtRouteNegotiatedContentResult<CustomerModel>)result;


            //Result contains the customer I had JUST createad
            result = cusomersContoller.GetCustomer(contentResult.Content.CustomerId);

            //Get CustomerModel from 'result'
            OkNegotiatedContentResult<CustomerModel> customerResult = (OkNegotiatedContentResult<CustomerModel>)result;


            //Act
            //The result of the Put Request
            result = cusomersContoller.PutCustomer(customerResult.Content.CustomerId, newCustomer);

            //Assert
            Assert.IsInstanceOfType(result, typeof(StatusCodeResult));

        }
コード例 #7
0
        public void GetAccountsforCustomerIDReurnAccounts()
        {
            //Arrange
            var customerController = new CustomersController();

            //Act
            IHttpActionResult result = customerController.GetAccountsForCustomer(1);

            //Assert
            //If action returns: NotFound()
            Assert.IsNotInstanceOfType(result, typeof(NotFoundResult));

            //If action returns: Ok()
            Assert.IsInstanceOfType(result, typeof(OkNegotiatedContentResult<IQueryable<AccountModel>>));

        }
コード例 #8
0
        public void GetCustomerReturnCustomer()
        {
            //Arrange
            var customerController = new CustomersController();

            //Act
            IHttpActionResult result = customerController.GetCustomer(1);


            //Assert
            //If action returns: NotFound()
            Assert.IsNotInstanceOfType(result, typeof(NotFoundResult));

            //If action returns: Ok()
            Assert.IsInstanceOfType(result, typeof(OkNegotiatedContentResult<CustomerModel>));

            //if action was returing data in the body like: Ok<string>("data: 12")
          //  OkNegotiatedContentResult<string> conNegResult = Assert.IsType<OkNegotiatedContentResult<string>>(actionResult);
          //  Assert.Equal("data: 12", conNegResult.Content);


        }
コード例 #9
0
        public void DeleteCustomerRecord()
        {
            //Arrange
            //Create Controller
            var customersController = new CustomersController();

            //Create a customer to be deleted
            var dbCustomer = new CustomerModel
            {
                FirstName = "Testy",
                LastName = "McTesterson",
                Address1 = "123 Main Street",
                Address2 = "Suite 2",
                City = "San Diego",
                States = "CA",
                Zip = "92101",
           
            };

            //Add 'new customer' to the DB using a POST
            //Save returned value as RESULT
            IHttpActionResult result = customersController.PostCustomer(dbCustomer);

            //Cast result as Content Result so that I can gather information from ContentResult
            CreatedAtRouteNegotiatedContentResult<CustomerModel> contentResult = (CreatedAtRouteNegotiatedContentResult<CustomerModel>)result;


            //Result contains the customer I had JUST createad
            result = customersController.GetCustomer(contentResult.Content.CustomerId);

            //Get CustomerModel from 'result'
            OkNegotiatedContentResult<CustomerModel> customerResult = (OkNegotiatedContentResult<CustomerModel>)result;


            //Act
            //The result of the Delete Request
             result = customersController.DeleteCustomer(customerResult.Content.CustomerId);

            //Assert

            //If action returns: NotFound()
            Assert.IsNotInstanceOfType(result, typeof(NotFoundResult));

            Assert.IsInstanceOfType(result, typeof(OkNegotiatedContentResult<CustomerModel>));         
        }
コード例 #10
0
        public void PutCustomerUpdatesCustomer()
        {
            int customerIdForTest = 1;

            //Arrange: Instantiate CustomersController so its methods can be called
            var customerController = new CustomersController();

            //Act:
            // Get an existing customer, change it, and
            //  pass it to PutCustomer

            IHttpActionResult result = customerController.GetCustomer(customerIdForTest);
            OkNegotiatedContentResult<CustomerModel> contentResult =
                (OkNegotiatedContentResult<CustomerModel>)result;
            CustomerModel updatedCustomer = (CustomerModel)contentResult.Content;

            string address1BeforeUpdate = updatedCustomer.Address1;
            string cityBeforeUpdate = updatedCustomer.City;
            string stateBeforeUpdate = updatedCustomer.State;
            string zipBeforeUpdate = updatedCustomer.Zip;

            updatedCustomer.Address1 = "567 State Street";
            updatedCustomer.City = "Philadelphia";
            updatedCustomer.State = "PA";
            updatedCustomer.Zip = "14378";

            result = customerController.PutCustomer
                                     (updatedCustomer.CustomerId, updatedCustomer);

            //Assert:
            // Verify that HTTP status code is OK
            // Get the customer and verify that it was updated

            var statusCode = (StatusCodeResult)result;

            Assert.IsTrue(statusCode.StatusCode == System.Net.HttpStatusCode.NoContent);

            result = customerController.GetCustomer(customerIdForTest);

            Assert.IsInstanceOfType(result,
                typeof(OkNegotiatedContentResult<CustomerModel>));

            OkNegotiatedContentResult<CustomerModel> readContentResult =
                (OkNegotiatedContentResult<CustomerModel>)result;
            updatedCustomer = (CustomerModel)readContentResult.Content;

            Assert.IsTrue(updatedCustomer.Address1 == "567 State Street");
            Assert.IsTrue(updatedCustomer.City == "Philadelphia");
            Assert.IsTrue(updatedCustomer.State == "PA");
            Assert.IsTrue(updatedCustomer.Zip == "14378");

            updatedCustomer.Address1 = address1BeforeUpdate;
            updatedCustomer.City = cityBeforeUpdate;
            updatedCustomer.State = stateBeforeUpdate;
            updatedCustomer.Zip = zipBeforeUpdate;

            /*
            updatedCustomer.Address1 = "123 Fake Street";
            updatedCustomer.City = "San Diego";
            updatedCustomer.State = "CA";
            updatedCustomer.Zip = "92101";
            */

            result = customerController.PutCustomer
                                 (updatedCustomer.CustomerId, updatedCustomer);
        }
コード例 #11
0
        public void PostCustomerCreatesCustomer()
        {
            //Arrange: Instantiate CustomersController so its methods can be called
            var customerController = new CustomersController();

            //Act:
            // Create a CustomerModel object populated with test data,
            //  and call PostCustomer
            var newCustomer = new CustomerModel
            {
                FirstName = "Testy",
                LastName = "McTesterson",
                Address1 = "Land of QA",
                Address2 = "34 Broome St",
                City = "San Francisco",
                State = "CA",
                Zip = "92456"
            };
            IHttpActionResult result = customerController.PostCustomer(newCustomer);

            //Assert:
            // Verify that the HTTP result is CreatedAtRouteNegotiatedContentResult
            // Verify that the HTTP result body contains a nonzero customer ID
            Assert.IsInstanceOfType
                (result, typeof(CreatedAtRouteNegotiatedContentResult<CustomerModel>));
            CreatedAtRouteNegotiatedContentResult<CustomerModel> contentResult =
                (CreatedAtRouteNegotiatedContentResult<CustomerModel>)result;
            Assert.IsTrue(contentResult.Content.CustomerId != 0);

            // Delete the test customer
            result = customerController.DeleteCustomer(contentResult.Content.CustomerId);
        }
コード例 #12
0
        public void GetCustomersReturnsCustomers()
        {
            //Arrange: Instantiate CustomersController so its methods can be called
            var customerController = new CustomersController();

            //Act: Call the GetCustomers method
            IEnumerable<CustomerModel> customers = customerController.GetCustomers();

            //Assert: Verify that an array was returned with at least one element
            Assert.IsTrue(customers.Count() > 0);
        }