简体   繁体   中英

What is the correct syntax to test an array using CollectionAssert in VS 2013?

Just learning how to conduct unit tests in c# using Visual Studio 2013. I have created a test which assigns specific floating numbers to an array but not sure how to test. I am attempting to use the CollectionAssert class but cannot get the syntax right.

Code snippet:

[TestMethod]
public void ArrayTest()
{
        float[] grades;
        grades = new float[2];

        grades[0] = 91f;
        grades[1] = 89.1f;

        foreach (float grade in grades)
            Console.WriteLine(grade);
}

I just want to test whether or not the grades will be printed to console.

As Grant asked, what are you trying to test? Just testing simple values, this works as expected

    [TestMethod]
    public void CheckFloats()
    {
        float[] array1 = new float[] { 1.0f, 2.0f, 3.0f };
        float[] array2 = new float[] { 4.0f, 5.0f, 6.0f };

        CollectionAssert.AreEqual(array1, GenerateFloats());
    }

    private float[] GenerateFloats()
    {
        return new float[] { 1.0f, 2.0f, 3.0f };
    }

It passes as written. It fails if swap array2 for array1 in the AreEqual call.

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