【问题标题】:Nunit testing while using object as parameter使用对象作为参数时进行 Nunit 测试
【发布时间】:2013-09-11 03:46:02
【问题描述】:

我有一个“患者信息”类,其中包括变量,例如名字、姓氏、年龄、体重、身高、性别……。这些变量中的每一个也都有 get 和 set 方法。 另一个类“程序有以下方法

       public double weightCalc(patientinfo p3)
       {
        if (p3.getGender() == "male" || (p3.getGender() == "MALE")
            { 
                //Calculate ideal body weight
                idealWeight = (50 + (2.3 * (p3.getheight() - 60)));
            }
            else
            {
                //Calculate ideal body weight
                idealWeight = (45.5 + (2.3 * (p3.getheight() - 60)));
            }
        return idealWeight;

我必须为此方法为 NUnit 准备一个测试用例。由于我是一个基础学习者,期待有关如何在测试类中传递 height 或 getheight 值的想法

【问题讨论】:

  • 性别从何而来?
  • 对不起,应该是p3.getGender()。我错过了。刚刚编辑。谢谢

标签: c# object parameters nunit


【解决方案1】:

看起来您的方法是可测试的。只需确保将 patientinfo 对象设置为必要的值即可

只要定义一个这样的测试

[Test]
public void Will_Calculate_Correct_Weight_For_Woman()
{
   patientinfo p3 = new patientinfo();//set appropruate values here
   //Example: p3.Gender = "Woman"; or p3.setGender("Woman");
   double result = someClass.weightCalc(p3);
   Assert.AreEqual(110,result);
}

您必须为您的 p3 实例指定适当的设置

【讨论】:

  • 我到了这里。但主要问题是我如何通过高度值进行测试。我是否必须向用户询问值“for testing too 吗?”听起来不合法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-18
  • 2015-07-23
  • 1970-01-01
  • 2010-09-15
相关资源
最近更新 更多