public JsonResult Calculate(ResistorViewModel resistorModel) { resistorModel.ComputedResult = ""; if (!ModelState.IsValid) { return(Json(new { Success = false, Result = "", Errors = ModelState.Values.SelectMany(x => x.Errors).Select(x => x.ErrorMessage).ToList() }, JsonRequestBehavior.AllowGet)); } else { try { float tolerance = 0f; double ohmValue = _resCalSvc.CalculateResistorValue(resistorModel.SelectedColorA, resistorModel.SelectedColorB, resistorModel.SelectedColorC, resistorModel.SelectedColorD); if (!string.IsNullOrEmpty(resistorModel.SelectedColorD)) { tolerance = _resCalSvc.GetTolerance(resistorModel.SelectedColorD); } string result = $"Resistor value: { ohmValue.ToSI() } Ω +/- { tolerance } %"; return(Json(new { Success = true, Result = result, Errors = "" }, JsonRequestBehavior.AllowGet)); } catch (ArgumentException ex) { resistorModel.ComputedResult = ""; return(Json(new { Success = false, Result = "", Errors = new List <string>(1) { ex.Message } }, JsonRequestBehavior.AllowGet)); } catch (Exception) { resistorModel.ComputedResult = ""; //logger.log exception return(Json(new { Success = false, Result = "", Errors = new List <string>(1) { "Unknown error occured." } }, JsonRequestBehavior.AllowGet)); } } }
public void CalculateResitorValue_Valid_CaseInsenstive_Inputs(string bandAColor, string bandBColor, string bandCColor, string bandDColor, double expectedValue) { double actualValue = _calcSvc.CalculateResistorValue(bandAColor, bandBColor, bandCColor, bandDColor); Assert.AreEqual(expectedValue, actualValue); }