public async Task <CalculatorResponse> Divide(List <double> inputs) { CalculatorResponse response = new CalculatorResponse(); try { _computeResolver.SetComputeStrategy(new DivideStrategy()); double result = await _computeResolver.Compute(inputs); response.SetResult(result, true, 200, new List <string>()); } catch (CalculatorException ex) { // the response code is 200 as this is a handled operation even if there was an exception response.SetResult(double.NaN, false, 200, new List <string>(new string[] { ex.Message })); } catch { // the response code is 500 as this is an unhandled exception which is generic response.SetResult(double.NaN, false, 500, new List <string>(new string[] { Resources.GenericException })); } return(response); }
public async Task DivideStrategy_Success_Handled() { List <double> inputs = new List <double> { 1, 2, 3 }; CalculatorResponse expectedResponse = new CalculatorResponse(); expectedResponse.SetResult(0.16666, true, 200, new List <string>()); var computeResolver = new Mock <IComputeResolver>(); computeResolver.Setup(cr => cr.Compute(inputs)).ReturnsAsync(0.16666); ICalculatorService calculator = new CalculatorService(computeResolver.Object); var result = await calculator.Add(inputs); expectedResponse.Should().BeEquivalentTo(result); }
public async Task MultiplyStrategy_Failure_Handled() { List <double> inputs = new List <double> { 1, 2, 3 }; CalculatorResponse expectedResponse = new CalculatorResponse(); expectedResponse.SetResult(double.NaN, false, 500, new List <string>() { Resources.GenericException }); var computeResolver = new Mock <IComputeResolver>(); computeResolver.Setup(cr => cr.Compute(inputs)).ThrowsAsync(new Exception(Resources.GenericException)); ICalculatorService calculator = new CalculatorService(computeResolver.Object); var result = await calculator.Add(inputs); expectedResponse.Should().BeEquivalentTo(result); }