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 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 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); }
/// <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); }