SlideShare une entreprise Scribd logo
1  sur  23
What’s Cuckoo?
@nazomikan
13年6月1日土曜日
誰
@nazomikan
Takuya Nakajima
nazomikan.com
npmjs.org/~nazomikan
Web Engineer @ Next.Co.,LTD
13年6月1日土曜日
今日はNodeのテストの話
13年6月1日土曜日
みんな大好きテストツール
• ci jenkins
• stub系ライブラリ sinon
• スペック書くやつ mocha, chai
13年6月1日土曜日
exports.sum = function () {
var args = Array.prototype.slice.call(arguments)
, i = 0, iz = args.length, res = 0;
for (; i < iz; i++) {
res += args[i];
}
return res;
};
// sum(1, 2, 3, 4)
// > 10
総和を求める処理
13年6月1日土曜日
let’s test
var assert = require(‘assert’);
describe(‘#sum’, function () {
it(‘引数で与えた数の総和を返すこと’, function () {
var testModule = require(‘./test.js’)
, actual = atestModule.sum(1, 2, 3, 4)
;
assert.equal(actual, 10); //pass!
});
});
13年6月1日土曜日
とっても簡単
13年6月1日土曜日
けど簡単にテストできない
やつもいる
13年6月1日土曜日
そう、それはexportsされて
ない処理
13年6月1日土曜日
var util = require(‘util’);
exports.testableMethod = function (a, b) {
var num = a + b;
return formatNumber(num);
};
function untestableMethod(num) {
return util.format(‘¥d円’, num);
}
untestable method
testable methodごしでしか
テストできない
13年6月1日土曜日
何故テストできないか
• requireでロードしたモジュールは
exportsされてないローカルプロパティ
にはアクセスできないから
13年6月1日土曜日
moduleスコープの実装
NativeModule.getSource = function(id) {
return NativeModule._source[id];
}
NativeModule.wrap = function(script) {
return NativeModule.wrapper[0] + script + NativeModule.wrapper[1];
};
NativeModule.wrapper = [
'(function (exports, require, module, __filename, __dirname) { ',
'n});'
];
NativeModule.prototype.compile = function() {
var source = NativeModule.getSource(this.id);
source = NativeModule.wrap(source);
..
};
13年6月1日土曜日
無名関数でラップされてるので外から
はローカル変数にアクセスできない
13年6月1日土曜日
Cuckoo
13年6月1日土曜日
what’s cuckoo?
• module内のローカル変数にずかずかと
踏み込める画期的なモジュール!!
• requireのモック化とかもできる
• npm install cuckoo
13年6月1日土曜日
さっきのやつ
var util = require(‘util’);
exports.testableMethod = function (a, b) {
var num = a + b;
return formatNumber(num);
};
function untestableMethod(num) {
return util.format(‘¥d円’, num);
}
13年6月1日土曜日
let’s cuckoo!
var assert = require(‘assert’)
, cuckoo = require(‘cuckoo’);
describe(‘#untestableMethod’, function () {
it(‘渡された値をutil.formatに置換文字列として渡す’, function () {
var target, mock = {};
mock.util = {
format: function (pattern, num) {
assert.equal(3, num); // pass
}
};
target = cuckoo.load(‘./test.js’, mock);
target.private.untestableMthod(3);
});
});
13年6月1日土曜日
実演
13年6月1日土曜日
cuckooのtodo
• globalメソッドも簡単にstub/mock化とか
• だれか使ってFBください。
13年6月1日土曜日
まとめ
• sinonやmochaやテストのためのツールは
結構はやってるけどそれでもuntestable
なメソッドはある
• そういうなんのだいたいはcuckooで解
決できる
13年6月1日土曜日
終わりに
• VMモジュールには魔術がつまってるの
で漁ってみるとフロントのJSじゃ到底む
りなことでもできたりするよ!
13年6月1日土曜日
おまけ
• 絵心のない僕がトラッ
クボールで12秒でつく
りました。
• だれかそれっぽいあれ
を作って!!
13年6月1日土曜日
13年6月1日土曜日

Contenu connexe

Tendances

Interactive Music II - SuperCollider入門
Interactive Music II - SuperCollider入門Interactive Music II - SuperCollider入門
Interactive Music II - SuperCollider入門Atsushi Tadokoro
 
すごい constexpr たのしくレイトレ!
すごい constexpr たのしくレイトレ!すごい constexpr たのしくレイトレ!
すごい constexpr たのしくレイトレ!Genya Murakami
 
菩薩でもわかる!Rで動かすExcelアドインの作り方
 菩薩でもわかる!Rで動かすExcelアドインの作り方  菩薩でもわかる!Rで動かすExcelアドインの作り方
菩薩でもわかる!Rで動かすExcelアドインの作り方 Nagi Teramo
 
Play frameworkをエヴァンジェる
Play frameworkをエヴァンジェるPlay frameworkをエヴァンジェる
Play frameworkをエヴァンジェるke-m kamekoopa
 
PCさえあればいい。
PCさえあればいい。PCさえあればいい。
PCさえあればいい。bleis tift
 
Constexpr 中3女子テクニック
Constexpr 中3女子テクニックConstexpr 中3女子テクニック
Constexpr 中3女子テクニックGenya Murakami
 
とびだせHaskellの森1
とびだせHaskellの森1とびだせHaskellの森1
とびだせHaskellの森1Shoko Sasaki
 
Interactive Music II SuperCollider入門 5 時間構造をつくる
Interactive Music II SuperCollider入門 5  時間構造をつくるInteractive Music II SuperCollider入門 5  時間構造をつくる
Interactive Music II SuperCollider入門 5 時間構造をつくるAtsushi Tadokoro
 

Tendances (8)

Interactive Music II - SuperCollider入門
Interactive Music II - SuperCollider入門Interactive Music II - SuperCollider入門
Interactive Music II - SuperCollider入門
 
すごい constexpr たのしくレイトレ!
すごい constexpr たのしくレイトレ!すごい constexpr たのしくレイトレ!
すごい constexpr たのしくレイトレ!
 
菩薩でもわかる!Rで動かすExcelアドインの作り方
 菩薩でもわかる!Rで動かすExcelアドインの作り方  菩薩でもわかる!Rで動かすExcelアドインの作り方
菩薩でもわかる!Rで動かすExcelアドインの作り方
 
Play frameworkをエヴァンジェる
Play frameworkをエヴァンジェるPlay frameworkをエヴァンジェる
Play frameworkをエヴァンジェる
 
PCさえあればいい。
PCさえあればいい。PCさえあればいい。
PCさえあればいい。
 
Constexpr 中3女子テクニック
Constexpr 中3女子テクニックConstexpr 中3女子テクニック
Constexpr 中3女子テクニック
 
とびだせHaskellの森1
とびだせHaskellの森1とびだせHaskellの森1
とびだせHaskellの森1
 
Interactive Music II SuperCollider入門 5 時間構造をつくる
Interactive Music II SuperCollider入門 5  時間構造をつくるInteractive Music II SuperCollider入門 5  時間構造をつくる
Interactive Music II SuperCollider入門 5 時間構造をつくる
 

En vedette

Borges -poema_a_los_amigos_+
Borges  -poema_a_los_amigos_+Borges  -poema_a_los_amigos_+
Borges -poema_a_los_amigos_+Hiraki Yami
 
1. h&m insurance dmj pcb
1. h&m insurance   dmj pcb1. h&m insurance   dmj pcb
1. h&m insurance dmj pcbshirishkirtane
 
Main ideas presentation
Main ideas presentationMain ideas presentation
Main ideas presentationjlr5466
 
Data Exploration and Information Retrieval in the B2C Marketing Department of...
Data Exploration and Information Retrieval in the B2C Marketing Department of...Data Exploration and Information Retrieval in the B2C Marketing Department of...
Data Exploration and Information Retrieval in the B2C Marketing Department of...Seun Odueko
 
New Life 2013 Summary Report FINAL
New Life 2013 Summary Report FINALNew Life 2013 Summary Report FINAL
New Life 2013 Summary Report FINALAlbert Christian Go
 
3 golongan yg dimasukan kedalam neraka
3 golongan yg dimasukan kedalam neraka3 golongan yg dimasukan kedalam neraka
3 golongan yg dimasukan kedalam nerakaDedi Samsuri
 
Shipping insurance seminar 25th jan at mumbai are we really insured ck
Shipping  insurance seminar 25th jan at mumbai   are we really insured ckShipping  insurance seminar 25th jan at mumbai   are we really insured ck
Shipping insurance seminar 25th jan at mumbai are we really insured ckshirishkirtane
 
1. dinyar jivaasha b. port operators insurance dmj pcb
1. dinyar jivaasha b. port operators insurance   dmj pcb1. dinyar jivaasha b. port operators insurance   dmj pcb
1. dinyar jivaasha b. port operators insurance dmj pcbshirishkirtane
 
Mini lesson
Mini lessonMini lesson
Mini lessonjlr5466
 
Main ideas presentation
Main ideas presentationMain ideas presentation
Main ideas presentationjlr5466
 
Galapagos islands
Galapagos islandsGalapagos islands
Galapagos islandsRuth Torres
 
Infierno contenido
Infierno contenidoInfierno contenido
Infierno contenidodaniel150
 

En vedette (17)

Borges -poema_a_los_amigos_+
Borges  -poema_a_los_amigos_+Borges  -poema_a_los_amigos_+
Borges -poema_a_los_amigos_+
 
Ro sssh...
Ro sssh...Ro sssh...
Ro sssh...
 
1. h&m insurance dmj pcb
1. h&m insurance   dmj pcb1. h&m insurance   dmj pcb
1. h&m insurance dmj pcb
 
Main ideas presentation
Main ideas presentationMain ideas presentation
Main ideas presentation
 
Data Exploration and Information Retrieval in the B2C Marketing Department of...
Data Exploration and Information Retrieval in the B2C Marketing Department of...Data Exploration and Information Retrieval in the B2C Marketing Department of...
Data Exploration and Information Retrieval in the B2C Marketing Department of...
 
Robin sathaye
Robin sathayeRobin sathaye
Robin sathaye
 
New Life 2013 Summary Report FINAL
New Life 2013 Summary Report FINALNew Life 2013 Summary Report FINAL
New Life 2013 Summary Report FINAL
 
3 golongan yg dimasukan kedalam neraka
3 golongan yg dimasukan kedalam neraka3 golongan yg dimasukan kedalam neraka
3 golongan yg dimasukan kedalam neraka
 
Shipping insurance seminar 25th jan at mumbai are we really insured ck
Shipping  insurance seminar 25th jan at mumbai   are we really insured ckShipping  insurance seminar 25th jan at mumbai   are we really insured ck
Shipping insurance seminar 25th jan at mumbai are we really insured ck
 
Tema 1
Tema 1Tema 1
Tema 1
 
1. dinyar jivaasha b. port operators insurance dmj pcb
1. dinyar jivaasha b. port operators insurance   dmj pcb1. dinyar jivaasha b. port operators insurance   dmj pcb
1. dinyar jivaasha b. port operators insurance dmj pcb
 
New Life 2012 Annual Report
New Life 2012 Annual ReportNew Life 2012 Annual Report
New Life 2012 Annual Report
 
Mini lesson
Mini lessonMini lesson
Mini lesson
 
Main ideas presentation
Main ideas presentationMain ideas presentation
Main ideas presentation
 
Galapagos islands
Galapagos islandsGalapagos islands
Galapagos islands
 
Unison opg (1)
Unison opg (1)Unison opg (1)
Unison opg (1)
 
Infierno contenido
Infierno contenidoInfierno contenido
Infierno contenido
 

What's Cuckoo?