このコードは動かない。
[Test] public IEnumerator APIAccessTest () { Dictionary <string, object> webInfo = new Dictionary<string, object> (); string userId = "00001"; // yield return StrarCoroutine (apiAccess.FireWebAPI (userId, (value) => { webInfo = value;})); yield return apiAccess.FireWebAPI (userId, (value) => { webInfo = value;}); Dictionary<string, object> result = new Dictionary<string, object>(); if (opponentInfo != null) { result = webInfo ["result"] as Dictionary<string, object>; } Assert.IsTrue (result.Count >= 0, "Result is equal and more than 0."); }
・実際にテストしたいコード
public class ApiAccess : MonoBehaviour { public IEnumerator FireWebAPI (string user_id, System.Action<Dictionary<string, object>> returnData) { WWWForm form = new WWWForm(); form.AddField ("user_id", user_id); WWW www = new WWW ("http://example.com", form); yield return StartCoroutine (WaitForRequest (www, returnData)); } private IEnumerator WaitForRequest (WWW www, System.Action<Dictionary<string, object>> returnData) { yield return www; if (www.error == null) { Dictionary<string, object> dict = Json.Deserialize (www.data) as Dictionary <string, object>; returnData (dict); } else { Debug.LogError("Error " + www.error + " URL " + www.url); returnData(null); } } }
↓修正後
[Test] public IEnumerator APIAccessTest () { Dictionary <string, object> webInfo = new Dictionary<string, object> (); string userId = "00001"; // StartCoroutineはこちらに書かない。 // yield return StrarCoroutine (apiAccess.FireWebAPI (userId, (value) => { webInfo = value;})); yield return apiAccess.FireWebAPI (userId, (value) => { webInfo = value;}); Dictionary<string, object> result = new Dictionary<string, object>(); if (opponentInfo != null) { result = webInfo ["result"] as Dictionary<string, object>; } Assert.IsTrue (result.Count >= 0, "Result is equal and more than 0."); }
public class ApiAccess : MonoBehaviour { public Coroutine FireWebAPI () { return StartCoroutine(FireWebAPIRoutine); } private IEnumerator FireWebAPIRoutine (string user_id, System.Action<Dictionary<string, object>> returnData) { WWWForm form = new WWWForm(); form.AddField ("user_id", user_id); WWW www = new WWW ("http://example.com", form); yield return StartCoroutine (WaitForRequest (www, returnData)); } private IEnumerator WaitForRequest (WWW www, System.Action<Dictionary<string, object>> returnData) { yield return www; if (www.error == null) { Dictionary<string, object> dict = Json.Deserialize (www.data) as Dictionary <string, object>; returnData (dict); } else { Debug.LogError("Error " + www.error + " URL " + www.url); returnData(null); } } }