public async Task AcquireSPTokenAsync_ExpectedResult() { //Arrange SetMockSetup_AcquireSPTokenAsync(); var handlerMock = new Mock <HttpMessageHandler>(); var response = new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent(@"{ ""id"": 101, ""access_token"" : ""asdsdfgsdfg"" }"), }; handlerMock .Protected() .Setup <Task <HttpResponseMessage> >( "SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>()) .ReturnsAsync(response); var httpClient = new HttpClient(handlerMock.Object); var spAuthenticationTokenService = new SPAuthenticationTokenService(httpClient, _mockSPClientServiceConfiguration, _mockLogger); //Act await spAuthenticationTokenService.AcquireSPTokenAsync(); //Assert handlerMock.Protected().Verify( "SendAsync", Times.Exactly(1), ItExpr.Is <HttpRequestMessage>(req => req.Method == HttpMethod.Post), ItExpr.IsAny <CancellationToken>()); Mock.Get(_mockSPClientServiceConfiguration).VerifyAll(); Mock.Get(_mockLogger).VerifyAll(); }
public void AcquireSPTokenAsync_ExpectedSPTokenAcquisitionFailureException() { //Arrange SetMockSetup_AcquireSPTokenAsync(); SetMockSetup_Logger(LogLevel.Error); var handlerMock = new Mock <HttpMessageHandler>(); var response = new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent(@"{ ""id"": 101, ""sdfg"" : ""asdsdfgsdfg"" }"), }; handlerMock .Protected() .Setup <Task <HttpResponseMessage> >( "SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>()) .ReturnsAsync(response); var httpClient = new HttpClient(handlerMock.Object); var spAuthenticationTokenService = new SPAuthenticationTokenService(httpClient, _mockSPClientServiceConfiguration, _mockLogger); //Act Func <Task> act = async() => await spAuthenticationTokenService.AcquireSPTokenAsync(); //Assert act.Should().Throw <SPTokenAcquisitionFailureException>(); handlerMock.Protected().Verify( "SendAsync", Times.Exactly(1), ItExpr.Is <HttpRequestMessage>(req => req.Method == HttpMethod.Post), ItExpr.IsAny <CancellationToken>()); Mock.Get(_mockSPClientServiceConfiguration).VerifyAll(); Mock.Get(_mockLogger).VerifyAll(); }