1
您好,我是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":"[email protected]", "secret":"da96195b4b", "server":"8526", "farm":9, "title":"Pod", "ispublic":1, "isfriend":0, "isfamily":0}, {"id":"8591810388", "owner":"[email protected]", "secret":"d94ce346a5", "server":"8509", "farm":9, "title":"Street Plate", "ispublic":1, "isfriend":0, "isfamily":0}, {"id":"8591801040", "owner":"[email protected]", "secret":"cb7b1e246a", "server":"8097", "farm":9, "title":"Stone pod", "ispublic":1, "isfriend":0, "isfamily":0}, {"id":"8590414659", "owner":"[email protected]", "secret":"fb49a25607", "server":"8094", "farm":9, "title":"Street pole", "ispublic":1, "isfriend":0, "isfamily":0}, {"id":"8590411479", "owner":"[email protected]", "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
參數爲假。我想讓這個工作不是假的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 : '[email protected]'
per_page : number
).done((data) =>
$.each data.photos.photo, (i, item) =>
@photos.push item
)
謝謝你的好意。
當我使用註釋掉線法線,錯誤'類型錯誤:無法讀取屬性「標題」的undefined'發生。我想這個jsonp請求會導致錯誤... – weed 2013-03-29 01:15:37