Sinon.js fakeServer 未使用响应方法触发回调

发布于 2025-01-07 08:10:18 字数 1757 浏览 1 评论 0原文

我正在尝试使用 Sinon.js 模拟 POST 请求的服务器响应。它似乎工作正常,除了不触发成功回调。

# In the Exercise model:

  submit: (query) ->
    callback = (data) -> alert(data)
    $.post(@url(), { query: query }, callback)

# In the Exercise spec:

  beforeEach ->
    @server = sinon.fakeServer.create()
    @server.respondWith('POST', @exercise.url(),
                        [ 200, { "Content-Type": "application/json" },
                        '[{ correct: true, result_set: {} }' ])

    @exercise.submit('select * from students')


  # passes
  it "request is a POST", ->
    expect(@server.requests[0].method).toEqual('POST')

  # passes
  it "submits to the correct url", ->
    expect(@server.requests[0].url).toEqual(@exercise.url())

  it "fires the callback", ->
    @server.respond()
   # no callback fired! but if I inspect the server object I can see that
   # the queue is empty, and the response is properly attached to the request object.
   # see the state below

# State of the Server object

{"requests":[
{"readyState":4,
 "requestHeaders":
    {"Content-Type":"application/x-www-form-urlencoded;charset=utf-8",
     "Accept":"*/*",
     "X-Requested-With":"XMLHttpRequest"},
 "requestBody":"query=select+*+from+students",
 "status":200,
 "statusText":"OK",
 "method":"POST",
 "url":"exercises/some_id",
 "async":true,
 "responseText":"[
    { correct: true,
      result_set: 
        {} }",
 "responseXML":null,
 "sendFlag":true,
 "errorFlag":false,
 "responseHeaders":
    {"Content-Type":"application/json"}}],
 "responses":[
 {"method":"POST",
 "url":"exercises/some_id",
 "response":[200,
    {"Content-Type":"application/json"},"[
    { correct: true,
      result_set: 
        {} }"]}],
  "queue":[]}

I am trying to mock the server response for a POST request using Sinon.js. It seems to work fine except that is does not fire the success callback.

# In the Exercise model:

  submit: (query) ->
    callback = (data) -> alert(data)
    $.post(@url(), { query: query }, callback)

# In the Exercise spec:

  beforeEach ->
    @server = sinon.fakeServer.create()
    @server.respondWith('POST', @exercise.url(),
                        [ 200, { "Content-Type": "application/json" },
                        '[{ correct: true, result_set: {} }' ])

    @exercise.submit('select * from students')


  # passes
  it "request is a POST", ->
    expect(@server.requests[0].method).toEqual('POST')

  # passes
  it "submits to the correct url", ->
    expect(@server.requests[0].url).toEqual(@exercise.url())

  it "fires the callback", ->
    @server.respond()
   # no callback fired! but if I inspect the server object I can see that
   # the queue is empty, and the response is properly attached to the request object.
   # see the state below

# State of the Server object

{"requests":[
{"readyState":4,
 "requestHeaders":
    {"Content-Type":"application/x-www-form-urlencoded;charset=utf-8",
     "Accept":"*/*",
     "X-Requested-With":"XMLHttpRequest"},
 "requestBody":"query=select+*+from+students",
 "status":200,
 "statusText":"OK",
 "method":"POST",
 "url":"exercises/some_id",
 "async":true,
 "responseText":"[
    { correct: true,
      result_set: 
        {} }",
 "responseXML":null,
 "sendFlag":true,
 "errorFlag":false,
 "responseHeaders":
    {"Content-Type":"application/json"}}],
 "responses":[
 {"method":"POST",
 "url":"exercises/some_id",
 "response":[200,
    {"Content-Type":"application/json"},"[
    { correct: true,
      result_set: 
        {} }"]}],
  "queue":[]}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初心未许 2025-01-14 08:10:18

它会触发错误回调,因为您的 JSON 无效: '[{ Correct: true, result_set: {} }' 尝试使用 http://jsonlint.com/ 来验证您的响应,或者使用 JSON.stringify,那么您无需担心。

It fires the error callback because your JSON is invalid: '[{ correct: true, result_set: {} }' Try using http://jsonlint.com/ to verify your responses, or use JSON.stringify, then you don't need to worry about it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文