public void Should_Delete_Customer_Thats_Not_Null_And_Return_Success_StatusCode() { // Arrange Guid _guid = Guid.NewGuid(); Customer originalCustomer = new Customer() { Id = _guid, Name = "Moe", Groupid = 1 }; Mock <ICustomerService> mockCustomerService = new Mock <ICustomerService>(); Mock <IOrderService> mockOrderService = new Mock <IOrderService>(); mockCustomerService.Setup(c => c.GetCustomerInfo(It.IsAny <string>())) .Returns(originalCustomer); mockCustomerService.Setup(x => x.DeleteCustomer(It.IsAny <Customer>())); CustomerApiController controller = new CustomerApiController(mockCustomerService.Object, mockOrderService.Object); // Act var result = controller.DeleteCustomer(_guid.ToString()); var checkResult = result as StatusCodeResult; // Assert Assert.IsNotNull(checkResult); Assert.AreEqual(200, checkResult.StatusCode); }
public void PutCustomerUpdatesRepository() { //// Arrange bool wasCalled = false; Guid customerKey = Guid.NewGuid(); AnonymousCustomer anonymousCustomer = CreateFakeCustomer(customerKey); var MockCustomerService = new Mock<ICustomerService>(); MockCustomerService.Setup(cs => cs.Save(anonymousCustomer, It.IsAny<bool>())).Callback(() => wasCalled = true); MerchelloContext merchelloContext = GetMerchelloContext(MockCustomerService.Object); CustomerApiController ctrl = new CustomerApiController(merchelloContext, tempUmbracoContext); ctrl.Request = new HttpRequestMessage(); ctrl.Request.Properties.Add(HttpPropertyKeys.HttpConfigurationKey, new HttpConfiguration()); //// Act HttpResponseMessage response = ctrl.PutCustomer(anonymousCustomer); //// Assert Assert.AreEqual(System.Net.HttpStatusCode.OK, response.StatusCode); Assert.True(wasCalled); }
public void GetCustomerThrowsWhenRepositoryReturnsNull() { // Arrange Guid customerKey = Guid.NewGuid(); var MockCustomerService = new Mock<ICustomerService>(); MockCustomerService.Setup(cs => cs.GetById(customerKey)).Returns((AnonymousCustomer)null); var MockServiceContext = new Mock<IServiceContext>(); MockServiceContext.SetupGet(sc => sc.CustomerService).Returns(MockCustomerService.Object); MerchelloContext merchelloContext = new MerchelloContext(MockServiceContext.Object); CustomerApiController ctrl = new CustomerApiController(merchelloContext, tempUmbracoContext); var ex = Assert.Throws<HttpResponseException>(() => ctrl.GetCustomer(Guid.Empty)); }
public void CreateComment_fail() { // Arrange var unitOfWork = new Mock <IUnitOfWork>(); var controller = new CustomerApiController(unitOfWork.Object); var model = new CommentModel(); var userIdentity = new ClaimsIdentity(); var userPrincipal = new ClaimsPrincipal(userIdentity); controller.ControllerContext = new ControllerContext { HttpContext = new DefaultHttpContext { User = userPrincipal } }; var eventsRepository = new Mock <IEventsRepository>(); var accountRepository = new Mock <IAccountRepository>(); unitOfWork.Setup(x => x.AccountRepository).Returns(accountRepository.Object); unitOfWork.Setup(x => x.EventsRepository).Returns(eventsRepository.Object); var commentsCollection = new List <Comment>(); eventsRepository.Setup(x => x.Get(model.EventId)).Returns(new Event { Comments = commentsCollection }); // Act var result = controller.CreateComment(model); // Assert Assert.IsType <ObjectResult>(result); ObjectResult objectResult = result as ObjectResult; Assert.True(objectResult.StatusCode == (int)HttpStatusCode.BadRequest); Assert.IsType <List <string> >(objectResult.Value); Assert.True((objectResult.Value as List <string>).Count > 0); }
public void GetCustomerByKeyReturnsCorrectItemFromRepository() { // Arrange int customerId = 20; AnonymousCustomer anonymousCustomer = CreateFakeCustomer(customerId); var MockCustomerService = new Mock<ICustomerService>(); MockCustomerService.Setup(cs => cs.GetById(customerId)).Returns(anonymousCustomer); MerchelloContext merchelloContext = GetMerchelloContext(MockCustomerService.Object); CustomerApiController ctrl = new CustomerApiController(merchelloContext, tempUmbracoContext); //// Act var result = ctrl.GetCustomer(customerId); //// Assert Assert.AreEqual(anonymousCustomer, result); }
public void Should_NOT_Delete_NonExistent_Customer_Return_Correct_StatusCode() { // ARRANGE Guid _guid = Guid.NewGuid(); Customer nonExistentCustomer = null; Mock <ICustomerService> mockCustomerService = new Mock <ICustomerService>(); Mock <IOrderService> mockOrderService = new Mock <IOrderService>(); mockCustomerService.Setup(c => c.GetCustomerInfo(It.IsAny <string>())) .Returns(nonExistentCustomer); CustomerApiController controller = new CustomerApiController(mockCustomerService.Object, mockOrderService.Object); // ACT var result = controller.DeleteCustomer(_guid.ToString()); var checkResult = result as BadRequestObjectResult; // ASSERT Assert.AreEqual(400, checkResult.StatusCode); }
public void Should_Not_Get_NonExistent_Customer_And_Return_Correct_Status_Code() { // Arrange Guid _guid = Guid.NewGuid(); Customer customer = null; Mock <ICustomerService> mockCustomerService = new Mock <ICustomerService>(); Mock <IOrderService> mockOrderService = new Mock <IOrderService>(); mockCustomerService.Setup(c => c.GetCustomerInfo(It.IsAny <string>())) .Returns(customer); CustomerApiController controller = new CustomerApiController(mockCustomerService.Object, mockOrderService.Object); // Act var result = controller.GetCustomerInfo(_guid.ToString()); var checkresult = result as ObjectResult; // Assert Assert.IsNotNull(checkresult); Assert.AreEqual(400, checkresult.StatusCode); Assert.AreEqual("Customer with Id " + _guid.ToString() + " does not exist", checkresult.Value.ToString()); }
/// <summary> /// Test to verify that the proper error response is returned on an error /// </summary> //[Test] public void PutCustomerReturns500WhenRepositoryUpdateReturnsError() { //// Arrange Guid customerKey = Guid.NewGuid(); AnonymousCustomer anonymousCustomer = CreateFakeCustomer(customerKey); var MockCustomerService = new Mock<ICustomerService>(); MockCustomerService.Setup(cs => cs.Save(anonymousCustomer, It.IsAny<bool>())).Throws<InvalidOperationException>(); MerchelloContext merchelloContext = GetMerchelloContext(MockCustomerService.Object); CustomerApiController ctrl = new CustomerApiController(merchelloContext, tempUmbracoContext); ctrl.Request = new HttpRequestMessage(); ctrl.Request.Properties.Add(HttpPropertyKeys.HttpConfigurationKey, new HttpConfiguration()); //// Act HttpResponseMessage response = ctrl.PutCustomer(anonymousCustomer); //// Assert Assert.AreEqual(System.Net.HttpStatusCode.NotFound, response.StatusCode); }
public void Should_Create_Customer_Return_Success_ResponseCode_And_Guid() { // Arrange Guid _guid = Guid.NewGuid(); Customer customer = new Customer() { Name = "Moe", Groupid = 1 }; Mock <ICustomerService> mockCustomerService = new Mock <ICustomerService>(); Mock <IOrderService> mockOrderService = new Mock <IOrderService>(); mockCustomerService.Setup(c => c.CreateCustomer(It.IsAny <Customer>())) .Returns(_guid); CustomerApiController controller = new CustomerApiController(mockCustomerService.Object, mockOrderService.Object); // Act var result = controller.CreateCustomer(customer); var checkresult = result as ObjectResult; // Assert Assert.IsNotNull(checkresult); Assert.AreEqual(_guid.ToString(), checkresult.Value.ToString()); Assert.AreEqual(200, checkresult.StatusCode); }