测试方法失败(VS 中的单元测试)

发布于 2024-12-25 06:48:14 字数 1546 浏览 0 评论 0原文

我正在建立一个进行单元测试的实用实验室。下面是我正在测试的应用程序(我正在测试所有方法和构造函数)。我的所有测试都完全接受,即测试一种名为“isScalene()”的方法,该方法检查三角形是否是不等边三角形(所有边都是唯一的)。

您会在底部找到失败的测试方法。当我将“等边”更改为 True 并将“scalene”更改为 False 时,它​​就通过了。应用程序中出了点问题,但我就是不知道它是什么(可能在“uniqueSides() 中)。

如果有人可以帮助我,我将不胜感激!

public class Triangle {
  double[] sides;

  public Triangle(double a, double b, double c) {
      if ((a <= 0) || (b <= 0) || (c <= 0)){
          throw new ArgumentOutOfRangeException("Sidorna måste vara större än 0.");
      }
    sides = new double[] { a, b, c };
  } 

  private int uniqueSides() {
    return sides.Distinct<double>().Count();
  }

  public bool isScalene() {
      if (uniqueSides() == 1){
          return true;
      }
      else{
          return false;
      }
  }

  public bool isEquilateral() {
      if (uniqueSides() == 3){
          return true;
      }
      else{
          return false;
      }
  }

  public bool isIsosceles() {
      if (uniqueSides() == 2){
          return true;
      }
      else{
          return false;
      }
  }
}

...

    [TestMethod()]
    public void isScalene3Test3()
    {
        Triangle triangle = new Triangle(25, 250, 2000);

        var isosceles = triangle.isIsosceles();
        var equalateral = triangle.isEquilateral();
        var scalene = triangle.isScalene();

        Assert.IsFalse(isosceles, "Test Isosceles");
        Assert.IsFalse(equalateral, "Test Equalateral");
        Assert.IsTrue(scalene, "Test Scalene");
    }

I'm working on a laboratory practical with unit-testing. Below is the application that i'm testing (i'm testing all the methods and the constructor). All my tests are complete accept for one, that is testing a method called "isScalene()", that checks wether the triangle is a scalene (all sides are unique) or not.

You find the test method that fails at the bottom. When I change "equalateral" to True and "scalene" to False, it passes. Something is wrong in the application, but I just can't figure out what it is (probably in "uniqueSides() though).

I would be grateful if someone could help me out!

public class Triangle {
  double[] sides;

  public Triangle(double a, double b, double c) {
      if ((a <= 0) || (b <= 0) || (c <= 0)){
          throw new ArgumentOutOfRangeException("Sidorna måste vara större än 0.");
      }
    sides = new double[] { a, b, c };
  } 

  private int uniqueSides() {
    return sides.Distinct<double>().Count();
  }

  public bool isScalene() {
      if (uniqueSides() == 1){
          return true;
      }
      else{
          return false;
      }
  }

  public bool isEquilateral() {
      if (uniqueSides() == 3){
          return true;
      }
      else{
          return false;
      }
  }

  public bool isIsosceles() {
      if (uniqueSides() == 2){
          return true;
      }
      else{
          return false;
      }
  }
}

...

    [TestMethod()]
    public void isScalene3Test3()
    {
        Triangle triangle = new Triangle(25, 250, 2000);

        var isosceles = triangle.isIsosceles();
        var equalateral = triangle.isEquilateral();
        var scalene = triangle.isScalene();

        Assert.IsFalse(isosceles, "Test Isosceles");
        Assert.IsFalse(equalateral, "Test Equalateral");
        Assert.IsTrue(scalene, "Test Scalene");
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

洒一地阳光 2025-01-01 06:48:14

isEquiside 应使用 1 个独特的边,isScalene 应使用 3 个。

public class Triangle {
  double[] sides;

  public Triangle(double a, double b, double c) {
      if ((a <= 0) || (b <= 0) || (c <= 0)){
          throw new ArgumentOutOfRangeException("Sidorna måste vara större än 0.");
      }
    sides = new double[] { a, b, c };
  } 

  private int uniqueSides() {
    return sides.Distinct<double>().Count();
  }

  public bool isScalene() {
      if (uniqueSides() == 3){
          return true;
      }
      else{
          return false;
      }
  }

  public bool isEquilateral() {
      if (uniqueSides() == 1){
          return true;
      }
      else{
          return false;
      }
  }

  public bool isIsosceles() {
      if (uniqueSides() == 2){
          return true;
      }
      else{
          return false;
      }
  }
}

isEquilateral should use 1 unique side, and isScalene should use 3.

public class Triangle {
  double[] sides;

  public Triangle(double a, double b, double c) {
      if ((a <= 0) || (b <= 0) || (c <= 0)){
          throw new ArgumentOutOfRangeException("Sidorna måste vara större än 0.");
      }
    sides = new double[] { a, b, c };
  } 

  private int uniqueSides() {
    return sides.Distinct<double>().Count();
  }

  public bool isScalene() {
      if (uniqueSides() == 3){
          return true;
      }
      else{
          return false;
      }
  }

  public bool isEquilateral() {
      if (uniqueSides() == 1){
          return true;
      }
      else{
          return false;
      }
  }

  public bool isIsosceles() {
      if (uniqueSides() == 2){
          return true;
      }
      else{
          return false;
      }
  }
}
懵少女 2025-01-01 06:48:14

您不想检查是否 (uniqueSides() == 3) 而不是 1 吗?

不管怎样,调试代码可以帮助你自己很快地发现这一点。

Don't you want to check if (uniqueSides() == 3) rather than 1?

Anyway, debugging the code would have helped you find this out very quickly for yourself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文