Last active
December 24, 2015 19:29
-
-
Save ishiduca/6851034 to your computer and use it in GitHub Desktop.
現在時刻に依存したモジュールのテスト ref: http://qiita.com/ishiduca/items/f2ac1fbda6cfe8c236f4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
TAP version 13 | |
# stub | |
# hook .toString | |
ok 1 (new Date).toSting() === "12" | |
ok 2 (new Date).toSting() === "hoge" | |
# reset .toString | |
ok 3 (new Date).toString() === "Sun Oct 06 2013 16:56:13 GMT+0900 (JST)" | |
1..3 | |
# tests 3 | |
# pass 3 | |
# ok | |
ok 127 ms | |
All tests successful. | |
Files=1, Tests=3, 0 wallclock secs ( 0.06 usr 0.01 sys + 0.06 cusr 0.02 csys = 0.15 CPU) | |
Result: PASS |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var test = require('tape') | |
test('stub for Date#toString', function (t) { | |
t.test('hook .toString', function (tt) { | |
var stub = {} | |
var toString | |
function setup () { | |
toString = Date.prototype.toString | |
Date.prototype.toString = function () { return stub.time } | |
} | |
function teardown () { | |
Date.prototype.toString = toString | |
stub.time = null | |
} | |
function subtest (time) { | |
stub.time = time | |
tt.equal((new Date).toString(), time | |
, '(new Date).toSting() === "' + time + '"') | |
} | |
setup() | |
subtest(12) | |
subtest('hoge') | |
teardown() | |
tt.end() | |
}) | |
t.test('reset .toString', function (tt) { | |
var d = new Date | |
var str = d.toString() | |
tt.ok(/\d\d:\d\d:\d\d\sGMT[+-]\d\d\d\d/.test(str) | |
, '(new Date).toString() === "' + str + '"') | |
tt.end() | |
}) | |
t.end() | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment