kidoOooOoooOOom

ゲーム開発やってます

phantom.jsとかいうやつ

テストの事前準備としてブラウザでの認証情報が必要な場合、phantom.jsを使ってCUIからブラウザ操作をして認証情報を取得していた。
基本的な操作はこんな感じ。

●require('webpage').create()
URL指定でページを表示するためのpageオブジェクトを作成します。

●page.open()
WEBページのURLを指定してページを表示します。

●page.evaluate()
page.openでページを表示した後にそのページ内で任意のJavaScriptを実行します。

●page.sendEvent()
domの位置を直接指定してClickイベントなどを発火させることができます。
イベントの発火はこのsendEventでのみ可能となっており、page.evaluateメソッドを使ってページ内でdispatchEventなどを実行してもイベントを発火させることはできません。

●page.render()
パスを指定してページのキャプチャを撮ります。
pdf,jpeg,pngに対応しており、パスの拡張子指定によって出力形式が変わります。

sinon.jsとかいうやつ

テストで便利なライブラリ。spy(), stub(), mock(), sandbox()などが提供されている。
この中のsandboxからのrestore()の動きが最初よく分からずにハマった。
mochaのbeforeEachでsanboxを確保してから各メソッドのstubを作っておいて、各テストにおいてstubを使わない場合はresotre()してからメソッドを呼び出すというテクニックを今日見かけた。

http://www.htmlhifive.com/conts/web/view/library/sinonjs#HSandbox

Sandboxを使用すると、restoreとverifyの呼び出しが簡略化されます。
また、グローバルにアクセス可能なfakeXHR、fakeTimerやStubを使用している場合は、Sandboxを使用することでクリーンアップが容易になります。

今日知ったasyncシリーズのやつ

・async.eachSeries
eachSeries(arr, iterator, callback)

The same as each only the iterator is applied to each item in the array in series. The next iterator is only called once the current one has completed processing. This means the iterator functions will complete in order.

arrに対し、非同期処理を含むiteratorを順番に実行させる。

async.waterfallで気をつけておくこと

JS学んでまだ日が浅いせいか、callbackの引数周りがよく混乱する。特に今日はasync.waterfallでcallbackの第1引数にはnull, 第2引数にidとかを設定しておいて、次の関数では第1引数でidを受け取っている感じになっていて、えええーーとなった。
第1引数がerrとして与えられる文化を覚えていかねば。。

async.waterfall([
  function(callback) {
    console.log('1');
    setTimeout(function() {
      console.log('1 done');
      callback(null, 1);
    }, 100);
  },
  function(arg1, callback) { // arg1 === 1
    console.log('2');
    setTimeout(function() {
      console.log('2 done');
      callback(null, 1, 2);
    }, 50);
  },
  function(arg1, arg2, callback) { // arg1 === 1, arg2 === 2
    console.log('3');
    setTimeout(function() {
      console.log('3 done');
      callback(null, 1, 2, 3);
    }, 10);
  }
], function(err, arg1, arg2, arg3) { // arg1 === 1, arg2 === 2, arg3 === 3
  if (err) {
    throw err;
  }
  console.log('all done.');
  console.log(arg1, arg2, arg3);
});

//出力結果
1
1 done
2
2 done
3
3 done
all done
1 2 3