DevLabs > DevLabs Forums > Pex > State Relation pattern
Ask a questionAsk a question
 

AnswerState Relation pattern

  • Thursday, October 29, 2009 11:58 PMrgopalan Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi,

    I am a bit confused about the "State Relation" pattern.
    In "Parameterized Test Patterns For Effective Testing with Pex" , The applicability of State Relation pattern says""This pattern applies when an API call causes an internal state change that can be (partially) observed through other API calls."

    I have the following PexMethod:

          [PexMethod]
          public void CanRegisterTwoObjectsAndGetThemBoth([PexAssumeUnderTest]A o,[PexAssumeUnderTest] object[] objects, [PexAssumeUnderTest] string[] keys)
             {
                ArrayList keylist = new ArrayList();
                 ArrayList objectlist = new ArrayList();
                for (int i = 0; i < keys.Length; i++)
                   {
                     if (!o.ContainsKey(keys[i]))
                    {
                        o.Add(keys[i], objects[i]);
                        keylist.Add(keys[i]);
                objectlist.Add(objects[i]);
                   }
            }
                 foreach (string key in keylist)
                      {               
               PexAssert.AreSame(o[key], objectlist[keylist.IndexOf(key)]);
            }  
          }
       
    I feel that this would fall under State Relation pattern because the "Add" operation on "o" is asserted by checking if "o" now has the object that was added at the index "key". But we do not use any API call here to observe this. Can you please tell me if this would still fall under state relation pattern or will it be some other pattern?

    Thanks!

Answers

  • Monday, November 02, 2009 6:45 AMNikolai TillmannMSFT, OwnerUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    You do use API calls to observe the resulted state change: In your assert, you have "objectlist[...]". This syntax is just syntactic sugar for calling the indexer getter method, defined by "TValue this[TKey index] { get { ... } }" in the code of the class "A".

    We might not explicitly mention this in the patterns document, but (implicit) calls to property and indexer getters and setters are also API calls.



    Nikolai Tillmann - Tell us how you use Pex

All Replies

  • Monday, November 02, 2009 6:45 AMNikolai TillmannMSFT, OwnerUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    You do use API calls to observe the resulted state change: In your assert, you have "objectlist[...]". This syntax is just syntactic sugar for calling the indexer getter method, defined by "TValue this[TKey index] { get { ... } }" in the code of the class "A".

    We might not explicitly mention this in the patterns document, but (implicit) calls to property and indexer getters and setters are also API calls.



    Nikolai Tillmann - Tell us how you use Pex
  • Monday, November 02, 2009 2:53 PMrgopalan Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thank you very much for clearing my confusion, Nikolai!