简体   繁体   English

单元测试失败 AreEquals() 和 ToString()

[英]Unit test fails AreEquals() and ToString()

My main method is:我的主要方法是:

using System;
namespace Rational
{
   public class Rational
   {
       public int Denom { get; private set; }
       public int Numer { get; private set; }
       public Rational(int numerator = 0, int denominator = 1)
       {
           Denom = denominator;
           Numer = numerator;
       }
       public void IncreaseBy(Rational num)
       {
           Numer = Numer * num.Denom + num.Denom * Denom;
           Denom = Denom * num.Denom;
       }
       public void DecreaseBy(Rational num)
       {
           Numer = Numer * num.Denom - num.Denom * Denom;
           Denom = Denom * num.Denom;
       }
       override public String ToString() { return (Numer + "/" + Denom); }
   }

My Program that calls on my method is:我调用我的方法的程序是:

using System;
namespace Rationals
{
   public class Rationals

   {

       class Program
       {
           static void Main(string[] args)
           {
               Rational num1 = new Rational(10, 8);
               Rational num2 = new Rational(8, 10);
               Console.WriteLine("1st number is:   " + num1);
               Console.WriteLine("2nd number is:   " + num2);
               num1.IncreaseBy(num2);
               Console.WriteLine("1st number + 2nd number = " + num1);
               num1.DecreaseBy(num2);
               Console.WriteLine("1st number - 2nd number = " + num2);
           }
       }
   }
}

I have to Write a Unit test to test my IncreaseBy() as well as my DecreaseBy() and finally ToString().我必须编写一个单元测试来测试我的IncreaseBy() 以及我的DecreaseBy() 以及最后的ToString()。 can someone please help me?!!?有人可以帮帮我吗?!!?

This is what i have so far in the UnitTest please crap on it as much as you want.这是我迄今为止在 UnitTest 中所拥有的,请尽可能多地使用它。 I AM UNABLE TO MAKE THE TEST PASS IT KEEPS COMING UP WITH A DIFFERENT ANSWER ALSO THE ToString() brings up an error.我无法使测试通过它不断出现不同的答案,而且 ToString() 会引发错误。

using Microsoft.VisualStudio.TestTools.UnitTesting;
using Rationals_123456789;

namespace ratonalstest
{
   [TestClass]
   public class UnitTest1
   {
       [TestMethod]
       public void DecreaseByTest()
       {
           int num = 1, den = 6, num1 = 2, den1 = 9;
           Rational fraction1 = new Rational(num, den);
           Rational fraction2 = new Rational(num1, den1);
           Rational expectedFraction = new Rational(-1, 18);

           //when
           fraction1.DecreaseBy(fraction2);

           //then
           Assert.AreEqual(fraction1.Numer, expectedFraction.Numer);
           Assert.AreEqual(fraction1.Denom, expectedFraction.Denom);

       }
       [TestMethod]
       public void IncreaseByTest()
       {
           int num = 1, den = 4, num1 = 1, den1 = 4;
           Rational fraction1 = new Rational(num, den);
           Rational fraction2 = new Rational(num1, den1);

           Rational expectedFraction = new Rational(7, 18);

           //when
           fraction1.IncreaseBy(fraction2);

           //then
           Assert.AreEqual(fraction1.Numer, expectedFraction.Numer);
           Assert.AreEqual(fraction1.Denom, expectedFraction.Denom);

           //when
           fraction1.DecreaseBy(fraction2);

           //then
           Assert.AreEqual(fraction1.Numer, expectedFraction.Numer);
           Assert.AreEqual(fraction1.Denom, expectedFraction.Denom);

       }

       [TestMethod]
       public void ToString()
       {
           int num = 1, den = 6, num1 = 2, den2 = 9;
           Rational fraction1 = new Rational(num, den);
           Rational fraction2 = new Rational(num1, den2);

           string expectedString = "1/6";
           string outputString = fraction1.ToString();

          //then
           Assert.AreEqual(expectedString, outputString);

       }
   }
}

There are two issues with your code.您的代码有两个问题。

  1. You calculate the result wrong你计算结果错误
  2. You don't normalize the fractions你没有标准化分数

First problem is that these two expressions:第一个问题是这两个表达式:

Numer = Numer * num.Denom + num.Denom * Denom;
Numer = Numer * num.Denom - num.Denom * Denom;

should use num.Numer instead of num.Denom so these are correct:应该使用num.Numer而不是num.Denom所以这些是正确的:

Numer = Numer * num.Denom + num.Numer * Denom;
Numer = Numer * num.Denom - num.Numer * Denom;

Now, this will still not make your tests pass, but let's try your code now:现在,这仍然不会使您的测试通过,但现在让我们尝试您的代码:

Rational fraction1 = new Rational(1, 6);
Rational fraction2 = new Rational(2, 9);

fraction1.DecreaseBy(fraction2);
Console.WriteLine(fraction1.ToString());

this outputs:这输出:

-3/54

Your test expects -1/18 , but these are in fact the same number.您的测试需要-1/18 ,但这些实际上是相同的数字。 Your tests have to handle this discrepancy, either by introducing fraction normalization or by comparing in a different manner.您的测试必须通过引入分数归一化或以不同的方式进行比较来处理这种差异。


Lastly I have some tips:最后我有一些tips:

  1. Make the Rational type a readonly immutable struct使 Rational 类型成为只读的不可变struct
  2. Implement proper operators that don't mutate the existing value but instead compute and return the result as a new Rational value实现不改变现有值而是计算结果并将结果作为新的 Rational 值返回的适当运算符
  3. Add support for normalization, this is usually done by dividing numerator and denominator by the GCD value, and making sure that at most only the numerator is negative (no negative denominator)添加对归一化的支持,这通常通过将分子和分母除以 GCD 值来完成,并确保最多只有分子是负数(没有负分母)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM