none
CollectionAssert.AllItemsAreInstancesOfType

    Question

  • Hi

               please help me with this

     [TestMethod]
            public void InputData()
            {
                IDictionary<string, string> myDictionary = new Dictionary<string, string>();

                myDictionary.Add("B", "BBBB");
                myDictionary.Add("C", "CCC");
                myDictionary.Add("D", "DDD");

    }
            PLEASE TELL ME WHAT CAN I CHECK USING THIS  "CollectionAssert.AllItemsAreInstancesOfType"

    ALSO PLEASE TELL ME HOW CAN I UNIT TEST THAT ALL THE KEYS IN THE DICTIONARY ARE OF TYPE "string"

    li2
          


    litu Here

    Thursday, October 31, 2013 1:08 AM

All replies

  • If it is not a string, it will show error during compile time itself

    litu Here

    Thursday, October 31, 2013 10:20 AM
  • Hi,

    AllItemsAreInstancesOfType Method (ICollection, Type) is used to verify that all elements in the specified collection are instances of the specified type. The assertion fails if for any element the type is not found in its inheritance hierarchy.

    The two parameters are collection and type.

    If you want to verify whether all keys in the Dictionary are Type string, you need to get all key in a collection using Keys Property and then compare its type with string type.

    Reference:

              IDictionary<string, string> myDictionary = new Dictionary<string, string>();

                myDictionary.Add("B", "BBBB");

                myDictionary.Add("C", "CCC");

                myDictionary.Add("D", "DDD");

                string para = "string";

                Type type1 = para.GetType();// get ‘string’ type

                ICollection coll = (ICollection)myDictionary.Keys;//get all keys and put them into a collection so that you can pass it into the AllItemsAreInstancesOfType method

              CollectionAssert.AllItemsAreInstancesOfType(coll,type1);

    You can try my code. Hope it can help you.

    If it is not a string, it will show error during compiling since you define the dictionary like IDictionary<string, string>.

    Best regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, November 01, 2013 5:27 AM
    Moderator