万年素人からHackerへの道

万年素人がHackerになれるまで殴り書きするぜ。

  • ・資産運用おすすめ
    10万円は1000円くらい利益
    資産運用ブログ アセマネ
    • ・寄付お願いします
      YENTEN:YYzNPzdsZWqr5THWAdMrKDj7GT8ietDc2W
      BitZenny:ZfpUbVya8MWQkjjGJMjA7P9pPkqaLnwPWH
      c0ban:8KG95GXdEquNpPW8xJAJf7nn5kbimQ5wj1
      Skycoin:KMqcn7x8REwwzMHPi9fV9fbNwdofYAWKRo

    UnTested Unity Coroutineでテスト。 C#

    このコードは動かない。

        [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);
            }
        }
    }