public async Task VerifyIfInitialCallFailsThatItRetries() { bool firstCall = true; var authService = new Mock <IAuthenticationService>(); authService.Setup(service => service.Authenticate(It.IsAny <string>(), It.IsAny <string>())) .Returns( async(string email, string password) => { if (firstCall) { firstCall = false; throw await HttpResponseException.CreateException(new HttpResponseMessage()); } }); var viewModel = new LoginViewModel(authService.Object) { Email = "test", Password = "******" }; authService.Verify(s => s.Authenticate("test", "test"), Times.Never); await viewModel.Authenticate(); authService.Verify(s => s.Authenticate("test", "test"), Times.Exactly(2)); // Twice cause the first time it throws, but it's still technically called. }
/// <summary> /// Throws if the <see cref="message"/>'s Status Code is unsuccessful (not 200OK or a variant thereof). /// </summary> /// <param name="message">The message.</param> /// <returns></returns> private static async Task <HttpResponseMessage> ThrowIfUnsuccessful(HttpResponseMessage message) { if (message.IsSuccessStatusCode) { return(message); } throw await HttpResponseException.CreateException(message); }
public async Task VerifyIfCallFailsSeveralTimesItThrowsTheException() { var authenticationService = new Mock <IAuthenticationService>(); authenticationService.Setup(service => service.Authenticate(It.IsAny <string>(), It.IsAny <string>())) .Returns( async(string email, string password) => { throw await HttpResponseException.CreateException(new HttpResponseMessage()); }); var viewModel = new LoginViewModel(authenticationService.Object) { Email = "test", Password = "******" }; await Should.ThrowAsync <HttpResponseException>(async() => await viewModel.Authenticate()); }