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":"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"

下面的代码很好地通过了测试,但是参数$.getJSONfake。我想让这个工作与非假 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
    )

谢谢你的好意。

4

1 回答 1

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"
于 2013-03-26T12:07:11.140 回答