Search Unity

Question Recommended way to test ISerializationCallbackReceiver implementations?

Discussion in 'Testing & Automation' started by jasonboukheir, Jul 18, 2020.

  1. jasonboukheir

    jasonboukheir

    Joined:
    May 3, 2017
    Posts:
    84
    I've got an implementation to
    ISerializationCallbackReceiver
    , and I wanted to test if my data was being saved properly. Is there a recommended way to do that?

    The part I'm having a difficult time understanding is handling the serialization lifecycle in a test. Assuming my implementation is called
    MyClass
    , ideally, I'd like to
    1. Create an asset that has
      MyClass
      attached to it.
    2. Make some edits to its serialized fields.
    3. Trigger a serialize of the
      SerializedObject
      .**
    4. Trigger a deserialize.**
    5. Get the instance of
      MyClass
      and validate the fields.
    I'm not sure how to go about doing steps 3 and 4.
     
  2. jasonboukheir

    jasonboukheir

    Joined:
    May 3, 2017
    Posts:
    84
    This is what I'm doing for now:
    Code (CSharp):
    1. public void SetTypeShouldPersistAfterCallingSave(Type expected)
    2. {
    3.     var asset = ScriptableObject.CreateInstance<SerializableTypeHolder>();
    4.     asset.Instance = new SerializableType(expected);
    5.     AssetDatabase.CreateAsset(asset, AssetPath);
    6.     asset = AssetDatabase.LoadAsset<SerializableTypeHolder>(AssetPath);
    7.     var actual = asset.Instance.Type;
    8.     Assert.AreEqual(expected, actual);
    9. }
     
    Last edited: Jul 18, 2020
  3. j-stash

    j-stash

    Joined:
    Jan 2, 2018
    Posts:
    1