Can't figure out why Assert.AreEqual fails

Lin 41 Reputation points
2022-10-31T16:43:46.767+00:00

I have a simple code that finds repeating words in a string(the use of StringBuilder is required). It may be not the most officiant one and certainly not the cleanest, but it does the bare minimum.
using System.Text.RegularExpressions;
using System.Text;

namespace StringsLib  
{  
    public class DoStrings  
    {  
        public static StringBuilder DoOne(string line)  
        {  
            char[] punctuationChars = { ' ', ',', '.', ':' };  
            string[] words = line.Split(punctuationChars, StringSplitOptions.RemoveEmptyEntries);  
  
            List<string> resultList = new List<string>();  
            for (int i = 0; i < words.Length; i++)  
            {  
                for (int j = 0; j < words.Length; j++)  
                {  
                    if (i != j)  
                    {  
                        if (words[i] == words[j] && !resultList.Contains(words[i]))  
                            resultList.Add(words[i]);  
                    }  
                }  
            }  
  
            StringBuilder result = new StringBuilder();  
            foreach (var word in resultList)  
                result.Append(word + " ");  
  
            Console.WriteLine("\nRepeating words:");  
            Console.WriteLine(result);  
  
            return result;  
        }  
}  

However I can't figure out why my test won't pass. Can somebody tell what's wrong?
using StringsLib;
using System.Text;

namespace StringsTest  
{  
    [TestClass]  
    public class UnitTest1  
    {  
        [TestMethod]  
        public void TestMethod1()  
        {  
            StringBuilder expected = new StringBuilder("боб ");  
              
            string line = "боб бобик бублик боб бибка. биба боба боб! боб?";  
            StringBuilder result = DoStrings.DoOne(line);  
  
            Assert.AreEqual(expected, result);  
        }  
}  
Visual Studio Testing
Visual Studio Testing
Visual Studio: A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.Testing: The act or process of applying tests as a means of analysis or diagnosis.
346 questions
{count} votes

Accepted answer
  1. Anna Xiu-MSFT 28,661 Reputation points Microsoft Vendor
    2022-11-04T13:44:35.797+00:00

    Hi @Lin ,

    Please try the following code:

    public void TestMethod1()  
                {  
      
                StringBuilder expected = new StringBuilder("боб ");  
      
                    string line = "боб бобик бублик боб бибка. биба боба боб! боб?";  
                    StringBuilder result = DoStrings.DoOne(line);  
      
                      
                    string result1 = result.ToString();  
                    string expected1 = expected.ToString();  
                      
      
                Assert.AreEqual(expected1, result1);  
                }  
    

    Assert.AreEqual method tests whether the strings are equal.

    Sincerely,
    Anna
    *
    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.