简体   繁体   中英

C# Unit Testing Lists

I'm trying to unit test in visual studios 2013,I know that using the method below with the supplied parameters will return a list containing the content of "Search term empty or contains invalid characters, please try again".

My method is

public List<string> SearchAnagram(string searchTerm,int userid)
    {  
        List<string> matchedAnagrams = new List<string>();

        matchedAnagrams.Add("Search term empty or contains invalid characters, please try again");      

        return matchedAnagrams;     
    }

test method below:

public void TestDigitSearchTerm()
    {
        List<string> matchedAnagrams = new List<string>();
        matchedAnagrams.Add("Search term empty or contains invalid characters, please try again");
        Assert.AreEqual(matchedAnagrams, anagram.SearchAnagram("gd32", 1));
    }

However what I receive is

Message: Assert.AreEqual failed. 
Expected:<System.Collections.Generic.List`1[System.String]>.
Actual:  <System.Collections.Generic.List`1[System.String]>. 

From what I can see they are they exact same so the test should of worked.

Any help would be appreciated

Try CollectionAssert :

CollectionAssert.AreEqual(matchedAnagrams, anagram.SearchAnagram("gd32", 1))

Or

CollectionAssert.AreEquivalent(matchedAnagrams, anagram.SearchAnagram("gd32", 1))

Ensure your visual studio is configured with the correct Target Framework. Double click on the project it will open the following window Preference window

Click on General option, select an installed target framework from the drop down and re-run your tests

Target Framework selection

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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