|
- /// <summary>
- /// 加法运算
- /// </summary>
- /// <param name="addend">加数</param>
- /// <param name="augend">被加数</param>
- /// <param name="msg">消息</param>
- /// <returns>运算结果</returns>
- public decimal Addition(string addend, string augend, out string msg)
- {
- msg = "";
- decimal a = 0;//加数
- decimal b = 0;//被加数
- try
- {
- if (!decimal.TryParse(addend, out a)) //转换加数
- {
- msg = "加数字符串转换失败。";
- return 0;
- }
- if (!decimal.TryParse(augend, out b)) //转换被加数
- {
- msg = "被加数字符串转换失败。";
- return 0;
- }
-
- }
- catch (Exception ex)
- {
- msg = ex.ToString();
- }
- return a + b;
- }
复制代码- /// <summary>
- /// 测试加法运算的结果
- /// </summary>
- [TestMethod]
- public void TestAddition_result()
- {
- SuperCalc.Calculate cl = new SuperCalc.Calculate();
- string msg = "";
- decimal expected = 0;
- decimal actual = 0;
- //正常结果
- Assert.AreEqual(50, cl.Addition("23", "27", out msg));
- Assert.AreEqual(-1, cl.Addition("-1", "0", out msg));
- //错误但能捕获到的结果
- //加数格式不对
- msg = "";
- expected = 0;
- actual = cl.Addition("ack", "145", out msg);
- Assert.AreEqual(expected, actual);
- Assert.AreEqual("加数字符串转换失败。", msg);
- //被加数格式不对
- msg = "";
- expected = 0;
- actual = cl.Addition("125", "adkc", out msg);
- Assert.AreEqual(expected, actual);
- Assert.AreEqual("被加数字符串转换失败。", msg);
- }
复制代码 为该方法写的单元测试无法达到100%,因为catch无法覆盖,
这个应该怎么处理呢。
|
|