您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

74 行
2.4 KiB

using System.Collections;
using UnityEngine;
using UnityEngine.TestTools;
using Assert = UnityEngine.Assertions.Assert;
namespace LobbyRelaySample.Tests
{
public class ObserverTests
{
[UnityTest]
public IEnumerator ObserverChangeWhenObservedChanged() // Test if Observer changes when StringField gets set
{
var observed = new TestObserved();
var observer = new GameObject("PlayerObserver").AddComponent<TestObserverBehaviour>();
observer.BeginObserving(observed);
Assert.AreNotEqual("NewName", observed.StringField);
Assert.AreNotEqual("NewName", observer.displayStringField);
observed.StringField = "NewName";
yield return null;
Assert.AreEqual(observed.StringField, observer.displayStringField);
}
[UnityTest]
public IEnumerator ObserverRegistersInitialChanges() // Test if Observer changes on Initialization
{
var observed = new TestObserved();
observed.StringField = "NewName"; // Set the field before we begin observing
var observer = new GameObject("PlayerObserver").AddComponent<TestObserverBehaviour>();
Assert.AreNotEqual(observed.StringField, observer.displayStringField);
observer.BeginObserving(observed);
yield return null;
Assert.AreEqual(observed.StringField, observer.displayStringField);
}
class TestObserved : Observed<TestObserved>
{
string m_stringField;
public string StringField
{
get => m_stringField;
set
{
m_stringField = value;
OnChanged(this);
}
}
public override void CopyObserved(TestObserved oldObserved)
{
m_stringField = oldObserved.StringField;
OnChanged(this);
}
}
//Mock UI Observer
class TestObserverBehaviour : ObserverBehaviour<TestObserved>
{
public string displayStringField;
protected override void UpdateObserver(TestObserved observed)
{
base.UpdateObserver(observed);
displayStringField = observed.StringField;
}
}
}
}