【问题标题】:Sinon.js fakeServer.request.respond fails when $.getJSON with jsonp used使用带有 jsonp 的 $.getJSON 时,Sinon.js fakeServer.request.respond 失败
【发布时间】:2013-03-15 23:10:51
【问题描述】:

您好,我是 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
    )

谢谢你的好意。

【问题讨论】:

    标签: javascript coffeescript jasmine sinon


    【解决方案1】:

    您必须在拨打电话之前定义服务器的响应。最好添加@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"
    

    【讨论】:

    • 当我将注释掉的行改为普通行时,出现错误TypeError: Cannot read property 'title' of undefined。我想jsonp请求会导致错误...
    猜你喜欢
    • 1970-01-01
    • 2012-11-04
    • 2013-08-10
    • 2012-05-24
    • 2014-05-26
    • 2014-01-31
    • 2018-09-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多