Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tatsuro-ueda/4255049 to your computer and use it in GitHub Desktop.
Save tatsuro-ueda/4255049 to your computer and use it in GitHub Desktop.
jQuery code test with Jasmine in CoffeeScript
# 「JS開発におけるTDDと自動テストツール利用の勘所 」より引用。
# http://www.slideshare.net/KojiNakamura/jstdd
# 元コード
$ ->
$("div li .button")
.on 'click', ->
$("div .contents").html("<span>"+$(this).data("mydata")+"</span>")
# テスト可能コード
$ ->
$("button") # HTML構造に依存しないidセレクタに変更
.on 'click', ->
clickHandler $("contents"), $(this).data("mydata")
clickHandler = (elm, data) ->
elm.html("<span>"+data+"</span>")
# テストコード
it "should call html() of passed element", ->
fakeObj =
html: jasmine.createSpy()
clickHandler(fakeObj, "hoge")
expect(fakeObj.html).toHaveBeenCalledWith("<span>hoge</span>")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment