こんにちは、sinon.js の初心者です。Jasmine BDD テスト コードを書いています。flickr から写真を取得する小さなアプリケーションを作りたいです。
describe "with stub", ->
beforeEach ->
@server = sinon.fakeServer.create()
@flickrPhotos = @flickr.photos
afterEach ->
@flickrPhotos = [] # remove photo data
it "[0].title should be Pod", ->
@flickr.getData 5, true
@server.requests[0].respond(
200
"Content-Type": "application/json"
'{"photos":{"page":1, "pages":726, "perpage":5, "total":"3630", "photo":[{"id":"8591804280", "owner":"77921082@N00", "secret":"da96195b4b", "server":"8526", "farm":9, "title":"Pod", "ispublic":1, "isfriend":0, "isfamily":0}, {"id":"8591810388", "owner":"77921082@N00", "secret":"d94ce346a5", "server":"8509", "farm":9, "title":"Street Plate", "ispublic":1, "isfriend":0, "isfamily":0}, {"id":"8591801040", "owner":"77921082@N00", "secret":"cb7b1e246a", "server":"8097", "farm":9, "title":"Stone pod", "ispublic":1, "isfriend":0, "isfamily":0}, {"id":"8590414659", "owner":"77921082@N00", "secret":"fb49a25607", "server":"8094", "farm":9, "title":"Street pole", "ispublic":1, "isfriend":0, "isfamily":0}, {"id":"8590411479", "owner":"77921082@N00", "secret":"9aab17d3a9", "server":"8370", "farm":9, "title":"Street plate", "ispublic":1, "isfriend":0, "isfamily":0}]}, "stat":"ok"}'
)
waitsFor (-> @flickrPhotos.length > 0), 'timeout', 1000
runs ->
expect(@flickrPhotos[0].title).toBe "Pod"
以下のコードはテストをうまくパスしますが、のパラメータ$.getJSON
はfakeです。偽のURLではなく、これを機能させたいです。
root = exports ? this
class root.Flickr
constructor: (@number) ->
@photos = []
getData: (number) ->
$.getJSON(
# 'http://www.flickr.com/services/rest/?jsoncallback=?' # true URL: fail
# fake request scceed only when without '?jsoncallback=?'
'http://www.flickr.com/services/rest/' # fake URL: succeed
format : 'json'
method : 'flickr.photos.search'
api_key : '7965a8bc5a2a88908e8321f3f56c80ea'
user_id : '77921082@N00'
per_page : number
).done((data) =>
$.each data.photos.photo, (i, item) =>
@photos.push item
)
ご親切にありがとうございました。