Created
December 11, 2012 01:41
-
-
Save tatsuro-ueda/4255049 to your computer and use it in GitHub Desktop.
jQuery code test with Jasmine in CoffeeScript
This file contains 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
# 「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