前回までで、phantomjsが大体わかったので、jasmine with CoffeeScriptな環境を作成します。
■説明
・CoffeeScriptのコンパイルを楽したい。ここではwatchrを利用してみます。
$ sudo gem install watchr
$ mkdir demo; cd demo
$ mkdir src; mkdir spec
$ vim coffee.watchr
watch(/(src|spec)\/.*\.coffee/) { |md|
system("coffee -cb #{md[0]}")
}
$ watchr coffee.watchr
(別の端末で)
$ vim src/test.coffee
console.log "a"
$ ls src
test.coffee test.js <= できてます!
・ではjasmineを利用してブラウザからテストを実行します。OOPでは古典的な"カウンター"を作ってみます。 $ wget http://pivotal.github.com/jasmine/downloads/jasmine-standalone-1.0.2.zip
$ unzip *.zip
$ rm *.zip
$ vim spec/CounterSpec.coffee
describe "Counter", ->
beforeEach ->
@counter = new Counter()
describe "#constructor", ->
it "の初期値は0である", ->
expect(@counter.value()).toEqual(0)
describe "#incr", ->
it "は値を1増加させるはず", ->
@counter.incr()
expect(@counter.value()).toEqual(1)
$ vim src/Counter.coffee
class Counter
constructor: () ->
@count = 0
incr: () ->
@count++
value: () ->
@count
$ vim SpecRunner.html (Counter.js/CounterSpec.jsの読み込みを記載する)
ブラウザで http://localhost/demo/SpecRunner.htmlを確認するといい感じです!。 最後にphantomjsからjasmineのSpecRunner.htmlを起動します。
$ vim src/run-jasmine.coffee
if phantom.state.length is 0
if phantom.args.length isnt 1
console.log 'Usage: run-jasmine.js URL'
phantom.exit()
else
phantom.state = 'run-jasmine'
phantom.open phantom.args[0]
else
window.setInterval ->
if document.body.querySelector('.finished-at')
console.log document.body.querySelector('.description').innerText
for el in document.body.querySelectorAll('div.jasmine_reporter > div.suite.failed')
console.log ''
for e in el.querySelectorAll('.description')
console.log e.innerText
phantom.exit()
, 100
$ phantomjs src/run-jasmine.coffee http://localhost/demo/SpecRunner.html
2 specs, 0 failures in 0.039s <= テスト結果が得られてます。いいじゃん!

0 件のコメント:
コメントを投稿