public void AccountCreation_Given_Model_Should_SendPOSTRequest() { var mockingKernel = new MoqMockingKernel(); //Arrange var mockConfig = mockingKernel.GetMock <IConfig>(); mockConfig.SetupGet(p => p.SimfyBaseUrl).Returns("SimfyBaseUrl"); mockConfig.SetupGet(p => p.SimfyApiUsername).Returns("SimfyApiUsername"); mockConfig.SetupGet(p => p.SimfyApiPassword).Returns("SimfyApiPassword"); mockConfig.SetupGet(p => p.SimfyEmail).Returns("SimfyEmail"); var mockHttpUtility = mockingKernel.GetMock <IHttpUtility>(); var service = new ThirdPartyServiceSimfy(mockingKernel); PrivateObject privateObject = new PrivateObject(service); var SimfyBaseUrl = privateObject.GetFieldOrProperty("SimfyBaseUrl", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic); var SimfyApiUsername = privateObject.GetFieldOrProperty("SimfyApiUsername", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic); var SimfyApiPassword = privateObject.GetFieldOrProperty("SimfyApiPassword", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic); var SimfyEmail = privateObject.GetFieldOrProperty("SimfyEmail", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic); var model = new SimfyAccountCreateRequest(); var simfy = SimfyBaseUrl.ToString() + "/create";; HttpResponseMessage value = new HttpResponseMessage(System.Net.HttpStatusCode.OK); SimfyAccountCreationResponse accountResponse = new SimfyAccountCreationResponse(); value.Content = new StringContent(JsonConvert.SerializeObject(accountResponse)); value.Content.Headers.Clear(); value.Content.Headers.Add("Content-Type", "application/json"); mockHttpUtility.Setup(p => p.PostRequest(simfy, "", model, null, false, 5)) .Returns(value); //Act service.AccountCreation(model); //Assert Assert.AreEqual(model.ApiLogin, SimfyApiUsername); Assert.AreEqual(model.ApiPassword, SimfyApiPassword); Assert.AreEqual(model.Email, model.MobileNumber + SimfyEmail); mockHttpUtility.Verify(p => p.PostRequest(simfy, "", model, null, false, 5), Times.Once); }
public void AccountCreation_Given_HTTPRequestException_Should_CatchException() { //Arrange var mockingKernel = new MoqMockingKernel(); var mockConfig = mockingKernel.GetMock <IConfig>(); mockConfig.SetupGet(p => p.SimfyBaseUrl).Returns("SimfyBaseUrl"); mockConfig.SetupGet(p => p.SimfyApiUsername).Returns("SimfyApiUsername"); mockConfig.SetupGet(p => p.SimfyApiPassword).Returns("SimfyApiPassword"); mockConfig.SetupGet(p => p.SimfyEmail).Returns("SimfyEmail"); var mockHttpUtility = mockingKernel.GetMock <IHttpUtility>(); var service = new ThirdPartyServiceSimfy(mockingKernel); PrivateObject privateObject = new PrivateObject(service); var SimfyBaseUrl = privateObject.GetFieldOrProperty("SimfyBaseUrl", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic); var SimfyApiUsername = privateObject.GetFieldOrProperty("SimfyApiUsername", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic); var SimfyApiPassword = privateObject.GetFieldOrProperty("SimfyApiPassword", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic); var SimfyEmail = privateObject.GetFieldOrProperty("SimfyEmail", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic); var model = new SimfyAccountCreateRequest(); var simfy = SimfyBaseUrl.ToString() + "/create";; HttpResponseMessage value = new HttpResponseMessage(System.Net.HttpStatusCode.InternalServerError); SimfyAccountCreationResponse accountResponse = new SimfyAccountCreationResponse() { Success = true }; value.Content = new StringContent(JsonConvert.SerializeObject(accountResponse)); value.Content.Headers.Clear(); value.Content.Headers.Add("Content-Type", "application/json"); mockHttpUtility.Setup(p => p.PostRequest(simfy, "", model, null, false, 5)) .Throws <Exception>(); //Act var responce = service.AccountCreation(model); //Assert mockHttpUtility.Verify(p => p.PostRequest(simfy, "", model, null, false, 5), Times.Once); Assert.IsFalse(responce.IsSuccess); Assert.AreEqual("Exception: Creating Simfy Account.", responce.Message); }