你好我是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
的参数是假。我想让这个工作不是伪造的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
)
谢谢你的善意。
答案 0 :(得分:0)
您必须在拨打电话之前定义服务器的响应。最好添加@server.autoRespond = true;
,这样你就不必等待测试了
describe "with stub", ->
beforeEach ->
@server = sinon.fakeServer.create()
@server.autoRespond = true;
@server.respondWith(
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"}'
)
@flickrPhotos = @flickr.photos
afterEach ->
@flickrPhotos = [] # remove photo data
it "[0].title should be Pod", ->
@flickr.getData 5, true
expect(@flickrPhotos[0].title).toBe "Pod"