Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 不使用派生对象的单元测试_C#_Unit Testing - Fatal编程技术网

C# 不使用派生对象的单元测试

C# 不使用派生对象的单元测试,c#,unit-testing,C#,Unit Testing,我正在尝试测试一个实现几个骰子表的函数。我已经推导了一个公式,目前我正在编写一组测试用例来测试正常操作,然后编写两个逻辑用例来防止结果越界 正在测试的功能是 public static int getNumberOfStars(Dice ourDice, int mod = 0) { int roll = ourDice.gurpsRoll() + mod; int numStars = (int)(Math.Floor((roll - 1.0)

我正在尝试测试一个实现几个骰子表的函数。我已经推导了一个公式,目前我正在编写一组测试用例来测试正常操作,然后编写两个逻辑用例来防止结果越界

正在测试的功能是

    public static int getNumberOfStars(Dice ourDice, int mod = 0)
    {
        int roll = ourDice.gurpsRoll() + mod;
        int numStars = (int)(Math.Floor((roll - 1.0) / 5.0));

        //fix a few possible logic bugs.
        if (numStars < 1) numStars = 1;
        else if (numStars > 3) numStars = 3;
        return numStars;           
    }
最后,测试用例是

    [TestMethod]
    public void VerifyMaxNumberOfStars()
    {
        FakeDice ourDice = new FakeDice();
        ourDice.diceRoll = 18;
        int numDice = StarReference.getNumberOfStars(ourDice, 3);
        Assert.AreEqual(3, numDice);
    }
运行之间结果会发生变化,这让我相信它没有使用新函数,而是使用基本函数。(此测试从未运行过,但手动运行代码会显示它返回有效结果。)


我做错了什么?

您的FakeDice类没有覆盖gurpsRoll()方法,而是隐藏了它

要覆盖实现,您需要在Dice类中将gurpsRoll声明为virtual,并且需要在FakeDice类中使用override关键字


您可以在这里的MSDN上找到有关此行为的更多详细信息

您的FakeDice类没有覆盖gurpsRoll()方法,而是将其隐藏

要覆盖实现,您需要在Dice类中将gurpsRoll声明为virtual,并且需要在FakeDice类中使用override关键字


您可以在这里的MSDN上找到有关此行为的更多详细信息

您的FakeDice类没有覆盖gurpsRoll()方法,而是将其隐藏

要覆盖实现,您需要在Dice类中将gurpsRoll声明为virtual,并且需要在FakeDice类中使用override关键字


您可以在这里的MSDN上找到有关此行为的更多详细信息

您的FakeDice类没有覆盖gurpsRoll()方法,而是将其隐藏

要覆盖实现,您需要在Dice类中将gurpsRoll声明为virtual,并且需要在FakeDice类中使用override关键字


您可以在这里的MSDN上找到有关此行为的更多详细信息,这是因为
新的
方法不会覆盖其基本方法

new
方法仅在处理声明它们的类时调用。如果您使用的是多态性,则将调用基方法

下面是一个例子:

FakeDice fake = new FakeDice();
fake.gurpsRoll();  // calls FakeDice's implementation

Dice dice = fake;
dice.gurpsRoll();  // calls Dice's implementation
您需要将基本方法设置为虚拟,然后重写它

见:


这是因为
新方法不会覆盖它们的基本方法

new
方法仅在处理声明它们的类时调用。如果您使用的是多态性,则将调用基方法

下面是一个例子:

FakeDice fake = new FakeDice();
fake.gurpsRoll();  // calls FakeDice's implementation

Dice dice = fake;
dice.gurpsRoll();  // calls Dice's implementation
您需要将基本方法设置为虚拟,然后重写它

见:


这是因为
新方法不会覆盖它们的基本方法

new
方法仅在处理声明它们的类时调用。如果您使用的是多态性,则将调用基方法

下面是一个例子:

FakeDice fake = new FakeDice();
fake.gurpsRoll();  // calls FakeDice's implementation

Dice dice = fake;
dice.gurpsRoll();  // calls Dice's implementation
您需要将基本方法设置为虚拟,然后重写它

见:


这是因为
新方法不会覆盖它们的基本方法

new
方法仅在处理声明它们的类时调用。如果您使用的是多态性,则将调用基方法

下面是一个例子:

FakeDice fake = new FakeDice();
fake.gurpsRoll();  // calls FakeDice's implementation

Dice dice = fake;
dice.gurpsRoll();  // calls Dice's implementation
您需要将基本方法设置为虚拟,然后重写它

见:


当使用
new
关键字重写方法时,它将仅在将对象装箱为派生对象时执行。您需要将要重写的方法设置为虚拟,如下所示:

public class FakeDice : Dice
{
      public int diceRoll;

      public override int gurpsRoll()
     {
         return diceRoll;
     }

  ...
}

public class Dice 
{
    public virtual int gurpsRoll() {...}
}

当使用
new
关键字重写方法时,它将仅在将对象装箱为派生对象时执行。您需要将要重写的方法设置为虚拟,如下所示:

public class FakeDice : Dice
{
      public int diceRoll;

      public override int gurpsRoll()
     {
         return diceRoll;
     }

  ...
}

public class Dice 
{
    public virtual int gurpsRoll() {...}
}

当使用
new
关键字重写方法时,它将仅在将对象装箱为派生对象时执行。您需要将要重写的方法设置为虚拟,如下所示:

public class FakeDice : Dice
{
      public int diceRoll;

      public override int gurpsRoll()
     {
         return diceRoll;
     }

  ...
}

public class Dice 
{
    public virtual int gurpsRoll() {...}
}

当使用
new
关键字重写方法时,它将仅在将对象装箱为派生对象时执行。您需要将要重写的方法设置为虚拟,如下所示:

public class FakeDice : Dice
{
      public int diceRoll;

      public override int gurpsRoll()
     {
         return diceRoll;
     }

  ...
}

public class Dice 
{
    public virtual int gurpsRoll() {...}
}

我觉得自己像个白痴。我完全忘记了虚拟/覆盖。@KoihimeNakamura我们都会犯错误,这就是为什么存在:)我觉得自己像个白痴。我完全忘记了虚拟/覆盖。@KoihimeNakamura我们都会犯错误,这就是为什么存在:)我觉得自己像个白痴。我完全忘记了虚拟/覆盖。@KoihimeNakamura我们都会犯错误,这就是为什么存在:)我觉得自己像个白痴。我完全忘记了虚拟/覆盖。@KoihimeNakamura我们都会犯错误,这就是为什么存在这样的错误:)